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, Pointer) { 107 LLVMContext C; 108 DataLayout DL(""); 109 110 for (unsigned AS : {0U, 1U, 127U, 0xffffU}) { 111 const LLT Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS)); 112 const LLT VTy = LLT::vector(4, Ty); 113 114 // Test kind. 115 ASSERT_TRUE(Ty.isValid()); 116 ASSERT_TRUE(Ty.isPointer()); 117 118 ASSERT_FALSE(Ty.isScalar()); 119 ASSERT_FALSE(Ty.isVector()); 120 121 ASSERT_TRUE(VTy.isValid()); 122 ASSERT_TRUE(VTy.isVector()); 123 ASSERT_TRUE(VTy.getElementType().isPointer()); 124 125 // Test addressspace. 126 EXPECT_EQ(AS, Ty.getAddressSpace()); 127 EXPECT_EQ(AS, VTy.getElementType().getAddressSpace()); 128 129 // Test equality operators. 130 EXPECT_TRUE(Ty == Ty); 131 EXPECT_FALSE(Ty != Ty); 132 EXPECT_TRUE(VTy == VTy); 133 EXPECT_FALSE(VTy != VTy); 134 135 // Test Type->LLT conversion. 136 Type *IRTy = PointerType::get(IntegerType::get(C, 8), AS); 137 EXPECT_EQ(Ty, getLLTForType(*IRTy, DL)); 138 Type *IRVTy = 139 VectorType::get(PointerType::get(IntegerType::get(C, 8), AS), 4); 140 EXPECT_EQ(VTy, getLLTForType(*IRVTy, DL)); 141 } 142 } 143 144 TEST(LowLevelTypeTest, Invalid) { 145 const LLT Ty; 146 147 ASSERT_FALSE(Ty.isValid()); 148 ASSERT_FALSE(Ty.isScalar()); 149 ASSERT_FALSE(Ty.isPointer()); 150 ASSERT_FALSE(Ty.isVector()); 151 } 152 153 } 154