1 //===- llvm/unittest/CodeGen/GlobalISel/LowLevelTypeTest.cpp --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 // Define a pretty printer to help debugging when things go wrong.
22 operator<<(std::ostream
&OS
, const llvm::LLT Ty
) {
24 raw_string_ostream SS
{Repr
};
33 TEST(LowLevelTypeTest
, Scalar
) {
37 for (unsigned S
: {1U, 17U, 32U, 64U, 0xfffffU
}) {
38 const LLT Ty
= LLT::scalar(S
);
41 ASSERT_TRUE(Ty
.isValid());
42 ASSERT_TRUE(Ty
.isScalar());
44 ASSERT_FALSE(Ty
.isPointer());
45 ASSERT_FALSE(Ty
.isVector());
48 EXPECT_EQ(S
, Ty
.getSizeInBits());
49 EXPECT_EQ(S
, Ty
.getScalarSizeInBits());
51 // Test equality operators.
52 EXPECT_TRUE(Ty
== Ty
);
53 EXPECT_FALSE(Ty
!= Ty
);
55 // Test Type->LLT conversion.
56 Type
*IRTy
= IntegerType::get(C
, S
);
57 EXPECT_EQ(Ty
, getLLTForType(*IRTy
, DL
));
61 TEST(LowLevelTypeTest
, Vector
) {
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
);
70 // Test the alternative vector().
72 const LLT VSTy
= LLT::vector(Elts
, STy
);
76 // Test getElementType().
77 EXPECT_EQ(STy
, VTy
.getElementType());
80 ASSERT_TRUE(VTy
.isValid());
81 ASSERT_TRUE(VTy
.isVector());
83 ASSERT_FALSE(VTy
.isScalar());
84 ASSERT_FALSE(VTy
.isPointer());
87 EXPECT_EQ(S
* Elts
, VTy
.getSizeInBits());
88 EXPECT_EQ(S
, VTy
.getScalarSizeInBits());
89 EXPECT_EQ(Elts
, VTy
.getNumElements());
91 // Test equality operators.
92 EXPECT_TRUE(VTy
== VTy
);
93 EXPECT_FALSE(VTy
!= VTy
);
95 // Test inequality operators on..
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
));
107 TEST(LowLevelTypeTest
, Pointer
) {
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
);
116 ASSERT_TRUE(Ty
.isValid());
117 ASSERT_TRUE(Ty
.isPointer());
119 ASSERT_FALSE(Ty
.isScalar());
120 ASSERT_FALSE(Ty
.isVector());
122 ASSERT_TRUE(VTy
.isValid());
123 ASSERT_TRUE(VTy
.isVector());
124 ASSERT_TRUE(VTy
.getElementType().isPointer());
126 // Test addressspace.
127 EXPECT_EQ(AS
, Ty
.getAddressSpace());
128 EXPECT_EQ(AS
, VTy
.getElementType().getAddressSpace());
130 // Test equality operators.
131 EXPECT_TRUE(Ty
== Ty
);
132 EXPECT_FALSE(Ty
!= Ty
);
133 EXPECT_TRUE(VTy
== VTy
);
134 EXPECT_FALSE(VTy
!= VTy
);
136 // Test Type->LLT conversion.
137 Type
*IRTy
= PointerType::get(IntegerType::get(C
, 8), AS
);
138 EXPECT_EQ(Ty
, getLLTForType(*IRTy
, DL
));
140 VectorType::get(PointerType::get(IntegerType::get(C
, 8), AS
), 4);
141 EXPECT_EQ(VTy
, getLLTForType(*IRVTy
, DL
));
145 TEST(LowLevelTypeTest
, Invalid
) {
148 ASSERT_FALSE(Ty
.isValid());
149 ASSERT_FALSE(Ty
.isScalar());
150 ASSERT_FALSE(Ty
.isPointer());
151 ASSERT_FALSE(Ty
.isVector());