1 //===- MachineIRBuilderTest.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 "GISelMITest.h" 10 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 11 12 TEST_F(GISelMITest, TestBuildConstantFConstant) { 13 if (!TM) 14 return; 15 16 B.buildConstant(LLT::scalar(32), 42); 17 B.buildFConstant(LLT::scalar(32), 1.0); 18 19 B.buildConstant(LLT::vector(2, 32), 99); 20 B.buildFConstant(LLT::vector(2, 32), 2.0); 21 22 // Test APFloat overload. 23 APFloat KVal(APFloat::IEEEdouble(), "4.0"); 24 B.buildFConstant(LLT::scalar(64), KVal); 25 26 auto CheckStr = R"( 27 CHECK: [[CONST0:%[0-9]+]]:_(s32) = G_CONSTANT i32 42 28 CHECK: [[FCONST0:%[0-9]+]]:_(s32) = G_FCONSTANT float 1.000000e+00 29 CHECK: [[CONST1:%[0-9]+]]:_(s32) = G_CONSTANT i32 99 30 CHECK: [[VEC0:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[CONST1]]:_(s32), [[CONST1]]:_(s32) 31 CHECK: [[FCONST1:%[0-9]+]]:_(s32) = G_FCONSTANT float 2.000000e+00 32 CHECK: [[VEC1:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[FCONST1]]:_(s32), [[FCONST1]]:_(s32) 33 CHECK: [[FCONST2:%[0-9]+]]:_(s64) = G_FCONSTANT double 4.000000e+00 34 )"; 35 36 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 37 } 38 39 40 #ifdef GTEST_HAS_DEATH_TEST 41 #ifndef NDEBUG 42 43 TEST_F(GISelMITest, TestBuildConstantFConstantDeath) { 44 if (!TM) 45 return; 46 47 LLVMContext &Ctx = MF->getFunction().getContext(); 48 APInt APV32(32, 12345); 49 50 // Test APInt version breaks 51 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), APV32), 52 "creating constant with the wrong size"); 53 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), APV32), 54 "creating constant with the wrong size"); 55 56 // Test ConstantInt version breaks 57 ConstantInt *CI = ConstantInt::get(Ctx, APV32); 58 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), *CI), 59 "creating constant with the wrong size"); 60 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), *CI), 61 "creating constant with the wrong size"); 62 63 APFloat DoubleVal(APFloat::IEEEdouble()); 64 ConstantFP *CF = ConstantFP::get(Ctx, DoubleVal); 65 EXPECT_DEATH(B.buildFConstant(LLT::scalar(16), *CF), 66 "creating fconstant with the wrong size"); 67 EXPECT_DEATH(B.buildFConstant(LLT::vector(2, 16), *CF), 68 "creating fconstant with the wrong size"); 69 } 70 71 #endif 72 #endif 73 74 TEST_F(GISelMITest, DstOpSrcOp) { 75 if (!TM) 76 return; 77 78 SmallVector<unsigned, 4> Copies; 79 collectCopies(Copies, MF); 80 81 LLT s64 = LLT::scalar(64); 82 auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]); 83 84 // Test SrcOp and DstOp can be constructed directly from MachineOperand by 85 // copying the instruction 86 B.buildAdd(MIBAdd->getOperand(0), MIBAdd->getOperand(1), MIBAdd->getOperand(2)); 87 88 89 auto CheckStr = R"( 90 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 91 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 92 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 93 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 94 )"; 95 96 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 97 } 98 99 TEST_F(GISelMITest, BuildUnmerge) { 100 if (!TM) 101 return; 102 103 SmallVector<unsigned, 4> Copies; 104 collectCopies(Copies, MF); 105 B.buildUnmerge(LLT::scalar(32), Copies[0]); 106 B.buildUnmerge(LLT::scalar(16), Copies[1]); 107 108 auto CheckStr = R"( 109 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 110 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 111 ; CHECK: [[UNMERGE32_0:%[0-9]+]]:_(s32), [[UNMERGE32_1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[COPY0]] 112 ; CHECK: [[UNMERGE16_0:%[0-9]+]]:_(s16), [[UNMERGE16_1:%[0-9]+]]:_(s16), [[UNMERGE16_2:%[0-9]+]]:_(s16), [[UNMERGE16_3:%[0-9]+]]:_(s16) = G_UNMERGE_VALUES [[COPY1]] 113 114 )"; 115 116 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 117 } 118 119 TEST_F(GISelMITest, TestBuildFPInsts) { 120 if (!TM) 121 return; 122 123 SmallVector<unsigned, 4> Copies; 124 collectCopies(Copies, MF); 125 126 LLT S64 = LLT::scalar(64); 127 128 B.buildFAdd(S64, Copies[0], Copies[1]); 129 B.buildFSub(S64, Copies[0], Copies[1]); 130 B.buildFMA(S64, Copies[0], Copies[1], Copies[2]); 131 B.buildFNeg(S64, Copies[0]); 132 B.buildFAbs(S64, Copies[0]); 133 B.buildFCopysign(S64, Copies[0], Copies[1]); 134 135 auto CheckStr = R"( 136 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 137 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 138 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2 139 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_ 140 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_ 141 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_ 142 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_ 143 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_ 144 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_ 145 )"; 146 147 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 148 } 149 150 TEST_F(GISelMITest, BuildIntrinsic) { 151 if (!TM) 152 return; 153 154 LLT S64 = LLT::scalar(64); 155 SmallVector<unsigned, 4> Copies; 156 collectCopies(Copies, MF); 157 158 // Make sure DstOp version works. sqrt is just a placeholder intrinsic. 159 B.buildIntrinsic(Intrinsic::sqrt, {S64}, false) 160 .addUse(Copies[0]); 161 162 // Make sure register version works 163 SmallVector<unsigned, 1> Results; 164 Results.push_back(MRI->createGenericVirtualRegister(S64)); 165 B.buildIntrinsic(Intrinsic::sqrt, Results, false) 166 .addUse(Copies[1]); 167 168 auto CheckStr = R"( 169 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 170 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 171 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_ 172 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_ 173 )"; 174 175 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 176 } 177 178 TEST_F(GISelMITest, BuildXor) { 179 if (!TM) 180 return; 181 182 LLT S64 = LLT::scalar(64); 183 LLT S128 = LLT::scalar(128); 184 SmallVector<unsigned, 4> Copies; 185 collectCopies(Copies, MF); 186 B.buildXor(S64, Copies[0], Copies[1]); 187 B.buildNot(S64, Copies[0]); 188 189 // Make sure this works with > 64-bit types 190 auto Merge = B.buildMerge(S128, {Copies[0], Copies[1]}); 191 B.buildNot(S128, Merge); 192 auto CheckStr = R"( 193 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 194 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 195 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_ 196 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1 197 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_ 198 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64) 199 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1 200 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_ 201 )"; 202 203 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 204 } 205 206 TEST_F(GISelMITest, BuildBitCounts) { 207 if (!TM) 208 return; 209 210 LLT S32 = LLT::scalar(32); 211 SmallVector<unsigned, 4> Copies; 212 collectCopies(Copies, MF); 213 214 B.buildCTPOP(S32, Copies[0]); 215 B.buildCTLZ(S32, Copies[0]); 216 B.buildCTLZ_ZERO_UNDEF(S32, Copies[1]); 217 B.buildCTTZ(S32, Copies[0]); 218 B.buildCTTZ_ZERO_UNDEF(S32, Copies[1]); 219 220 auto CheckStr = R"( 221 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 222 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 223 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_ 224 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_ 225 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_ 226 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_ 227 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_ 228 )"; 229 230 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 231 } 232