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 setUp(); 14 if (!TM) 15 return; 16 17 B.buildConstant(LLT::scalar(32), 42); 18 B.buildFConstant(LLT::scalar(32), 1.0); 19 20 B.buildConstant(LLT::vector(2, 32), 99); 21 B.buildFConstant(LLT::vector(2, 32), 2.0); 22 23 // Test APFloat overload. 24 APFloat KVal(APFloat::IEEEdouble(), "4.0"); 25 B.buildFConstant(LLT::scalar(64), KVal); 26 27 auto CheckStr = R"( 28 CHECK: [[CONST0:%[0-9]+]]:_(s32) = G_CONSTANT i32 42 29 CHECK: [[FCONST0:%[0-9]+]]:_(s32) = G_FCONSTANT float 1.000000e+00 30 CHECK: [[CONST1:%[0-9]+]]:_(s32) = G_CONSTANT i32 99 31 CHECK: [[VEC0:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[CONST1]]:_(s32), [[CONST1]]:_(s32) 32 CHECK: [[FCONST1:%[0-9]+]]:_(s32) = G_FCONSTANT float 2.000000e+00 33 CHECK: [[VEC1:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[FCONST1]]:_(s32), [[FCONST1]]:_(s32) 34 CHECK: [[FCONST2:%[0-9]+]]:_(s64) = G_FCONSTANT double 4.000000e+00 35 )"; 36 37 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 38 } 39 40 41 #ifdef GTEST_HAS_DEATH_TEST 42 #ifndef NDEBUG 43 44 TEST_F(GISelMITest, TestBuildConstantFConstantDeath) { 45 setUp(); 46 if (!TM) 47 return; 48 49 LLVMContext &Ctx = MF->getFunction().getContext(); 50 APInt APV32(32, 12345); 51 52 // Test APInt version breaks 53 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), APV32), 54 "creating constant with the wrong size"); 55 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), APV32), 56 "creating constant with the wrong size"); 57 58 // Test ConstantInt version breaks 59 ConstantInt *CI = ConstantInt::get(Ctx, APV32); 60 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), *CI), 61 "creating constant with the wrong size"); 62 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), *CI), 63 "creating constant with the wrong size"); 64 65 APFloat DoubleVal(APFloat::IEEEdouble()); 66 ConstantFP *CF = ConstantFP::get(Ctx, DoubleVal); 67 EXPECT_DEATH(B.buildFConstant(LLT::scalar(16), *CF), 68 "creating fconstant with the wrong size"); 69 EXPECT_DEATH(B.buildFConstant(LLT::vector(2, 16), *CF), 70 "creating fconstant with the wrong size"); 71 } 72 73 #endif 74 #endif 75 76 TEST_F(GISelMITest, DstOpSrcOp) { 77 if (!TM) 78 return; 79 80 SmallVector<Register, 4> Copies; 81 collectCopies(Copies, MF); 82 83 LLT s64 = LLT::scalar(64); 84 auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]); 85 86 // Test SrcOp and DstOp can be constructed directly from MachineOperand by 87 // copying the instruction 88 B.buildAdd(MIBAdd->getOperand(0), MIBAdd->getOperand(1), MIBAdd->getOperand(2)); 89 90 91 auto CheckStr = R"( 92 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 93 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 94 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 95 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 96 )"; 97 98 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 99 } 100 101 TEST_F(GISelMITest, BuildUnmerge) { 102 if (!TM) 103 return; 104 105 SmallVector<Register, 4> Copies; 106 collectCopies(Copies, MF); 107 B.buildUnmerge(LLT::scalar(32), Copies[0]); 108 B.buildUnmerge(LLT::scalar(16), Copies[1]); 109 110 auto CheckStr = R"( 111 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 112 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 113 ; CHECK: [[UNMERGE32_0:%[0-9]+]]:_(s32), [[UNMERGE32_1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[COPY0]] 114 ; 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]] 115 116 )"; 117 118 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 119 } 120 121 TEST_F(GISelMITest, TestBuildFPInsts) { 122 if (!TM) 123 return; 124 125 SmallVector<Register, 4> Copies; 126 collectCopies(Copies, MF); 127 128 LLT S64 = LLT::scalar(64); 129 130 B.buildFAdd(S64, Copies[0], Copies[1]); 131 B.buildFSub(S64, Copies[0], Copies[1]); 132 B.buildFMA(S64, Copies[0], Copies[1], Copies[2]); 133 B.buildFNeg(S64, Copies[0]); 134 B.buildFAbs(S64, Copies[0]); 135 B.buildFCopysign(S64, Copies[0], Copies[1]); 136 137 auto CheckStr = R"( 138 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 139 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 140 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2 141 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_ 142 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_ 143 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_ 144 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_ 145 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_ 146 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_ 147 )"; 148 149 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 150 } 151 152 TEST_F(GISelMITest, BuildIntrinsic) { 153 if (!TM) 154 return; 155 156 LLT S64 = LLT::scalar(64); 157 SmallVector<Register, 4> Copies; 158 collectCopies(Copies, MF); 159 160 // Make sure DstOp version works. sqrt is just a placeholder intrinsic. 161 B.buildIntrinsic(Intrinsic::sqrt, {S64}, false) 162 .addUse(Copies[0]); 163 164 // Make sure register version works 165 SmallVector<Register, 1> Results; 166 Results.push_back(MRI->createGenericVirtualRegister(S64)); 167 B.buildIntrinsic(Intrinsic::sqrt, Results, false) 168 .addUse(Copies[1]); 169 170 auto CheckStr = R"( 171 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 172 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 173 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_ 174 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_ 175 )"; 176 177 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 178 } 179 180 TEST_F(GISelMITest, BuildXor) { 181 if (!TM) 182 return; 183 184 LLT S64 = LLT::scalar(64); 185 LLT S128 = LLT::scalar(128); 186 SmallVector<Register, 4> Copies; 187 collectCopies(Copies, MF); 188 B.buildXor(S64, Copies[0], Copies[1]); 189 B.buildNot(S64, Copies[0]); 190 191 // Make sure this works with > 64-bit types 192 auto Merge = B.buildMerge(S128, {Copies[0], Copies[1]}); 193 B.buildNot(S128, Merge); 194 auto CheckStr = R"( 195 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 196 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 197 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_ 198 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1 199 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_ 200 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64) 201 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1 202 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_ 203 )"; 204 205 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 206 } 207 208 TEST_F(GISelMITest, BuildBitCounts) { 209 if (!TM) 210 return; 211 212 LLT S32 = LLT::scalar(32); 213 SmallVector<Register, 4> Copies; 214 collectCopies(Copies, MF); 215 216 B.buildCTPOP(S32, Copies[0]); 217 B.buildCTLZ(S32, Copies[0]); 218 B.buildCTLZ_ZERO_UNDEF(S32, Copies[1]); 219 B.buildCTTZ(S32, Copies[0]); 220 B.buildCTTZ_ZERO_UNDEF(S32, Copies[1]); 221 222 auto CheckStr = R"( 223 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 224 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 225 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_ 226 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_ 227 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_ 228 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_ 229 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_ 230 )"; 231 232 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 233 } 234 235 TEST_F(GISelMITest, BuildCasts) { 236 if (!TM) 237 return; 238 239 LLT S32 = LLT::scalar(32); 240 SmallVector<Register, 4> Copies; 241 collectCopies(Copies, MF); 242 243 B.buildUITOFP(S32, Copies[0]); 244 B.buildSITOFP(S32, Copies[0]); 245 B.buildFPTOUI(S32, Copies[0]); 246 B.buildFPTOSI(S32, Copies[0]); 247 248 auto CheckStr = R"( 249 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 250 ; CHECK: [[UITOFP:%[0-9]+]]:_(s32) = G_UITOFP [[COPY0]]:_ 251 ; CHECK: [[SITOFP:%[0-9]+]]:_(s32) = G_SITOFP [[COPY0]]:_ 252 ; CHECK: [[FPTOUI:%[0-9]+]]:_(s32) = G_FPTOUI [[COPY0]]:_ 253 ; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[COPY0]]:_ 254 )"; 255 256 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 257 } 258 259 TEST_F(GISelMITest, BuildMinMax) { 260 if (!TM) 261 return; 262 263 LLT S64 = LLT::scalar(64); 264 SmallVector<Register, 4> Copies; 265 collectCopies(Copies, MF); 266 267 B.buildSMin(S64, Copies[0], Copies[1]); 268 B.buildSMax(S64, Copies[0], Copies[1]); 269 B.buildUMin(S64, Copies[0], Copies[1]); 270 B.buildUMax(S64, Copies[0], Copies[1]); 271 272 auto CheckStr = R"( 273 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 274 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 275 ; CHECK: [[SMIN0:%[0-9]+]]:_(s64) = G_SMIN [[COPY0]]:_, [[COPY1]]:_ 276 ; CHECK: [[SMAX0:%[0-9]+]]:_(s64) = G_SMAX [[COPY0]]:_, [[COPY1]]:_ 277 ; CHECK: [[UMIN0:%[0-9]+]]:_(s64) = G_UMIN [[COPY0]]:_, [[COPY1]]:_ 278 ; CHECK: [[UMAX0:%[0-9]+]]:_(s64) = G_UMAX [[COPY0]]:_, [[COPY1]]:_ 279 )"; 280 281 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 282 } 283 284 TEST_F(GISelMITest, BuildAtomicRMW) { 285 if (!TM) 286 return; 287 288 LLT S64 = LLT::scalar(64); 289 LLT P0 = LLT::pointer(0, 64); 290 SmallVector<Register, 4> Copies; 291 collectCopies(Copies, MF); 292 293 MachineMemOperand *MMO = 294 MF->getMachineMemOperand( 295 MachinePointerInfo(), 296 MachineMemOperand::MOLoad | MachineMemOperand::MOStore, 297 8, 8, AAMDNodes(), nullptr, SyncScope::System, AtomicOrdering::Unordered); 298 299 auto Ptr = B.buildUndef(P0); 300 B.buildAtomicRMWFAdd(S64, Ptr, Copies[0], *MMO); 301 B.buildAtomicRMWFSub(S64, Ptr, Copies[0], *MMO); 302 303 auto CheckStr = R"( 304 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 305 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 306 ; CHECK: [[PTR:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF 307 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_ATOMICRMW_FADD [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8) 308 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_ATOMICRMW_FSUB [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8) 309 )"; 310 311 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 312 } 313