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("");
124 
125   for (unsigned AS : {0U, 1U, 127U, 0xffffU}) {
126     const LLT Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
127     const LLT VTy = LLT::vector(4, Ty);
128 
129     // Test kind.
130     ASSERT_TRUE(Ty.isValid());
131     ASSERT_TRUE(Ty.isPointer());
132 
133     ASSERT_FALSE(Ty.isScalar());
134     ASSERT_FALSE(Ty.isVector());
135 
136     ASSERT_TRUE(VTy.isValid());
137     ASSERT_TRUE(VTy.isVector());
138     ASSERT_TRUE(VTy.getElementType().isPointer());
139 
140     // Test addressspace.
141     EXPECT_EQ(AS, Ty.getAddressSpace());
142     EXPECT_EQ(AS, VTy.getElementType().getAddressSpace());
143 
144     // Test equality operators.
145     EXPECT_TRUE(Ty == Ty);
146     EXPECT_FALSE(Ty != Ty);
147     EXPECT_TRUE(VTy == VTy);
148     EXPECT_FALSE(VTy != VTy);
149 
150     // Test Type->LLT conversion.
151     Type *IRTy = PointerType::get(IntegerType::get(C, 8), AS);
152     EXPECT_EQ(Ty, getLLTForType(*IRTy, DL));
153     Type *IRVTy =
154         VectorType::get(PointerType::get(IntegerType::get(C, 8), AS), 4);
155     EXPECT_EQ(VTy, getLLTForType(*IRVTy, DL));
156   }
157 }
158 
159 TEST(LowLevelTypeTest, Invalid) {
160   const LLT Ty;
161 
162   ASSERT_FALSE(Ty.isValid());
163   ASSERT_FALSE(Ty.isScalar());
164   ASSERT_FALSE(Ty.isPointer());
165   ASSERT_FALSE(Ty.isVector());
166 }
167 
168 }
169