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.buildFMAD(S64, Copies[0], Copies[1], Copies[2]); 134 B.buildFMAD(S64, Copies[0], Copies[1], Copies[2], MachineInstr::FmNoNans); 135 B.buildFNeg(S64, Copies[0]); 136 B.buildFAbs(S64, Copies[0]); 137 B.buildFCopysign(S64, Copies[0], Copies[1]); 138 139 auto CheckStr = R"( 140 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 141 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 142 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2 143 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_ 144 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_ 145 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_ 146 ; CHECK: [[FMAD0:%[0-9]+]]:_(s64) = G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_ 147 ; CHECK: [[FMAD1:%[0-9]+]]:_(s64) = nnan G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_ 148 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_ 149 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_ 150 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_ 151 )"; 152 153 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 154 } 155 156 TEST_F(GISelMITest, BuildIntrinsic) { 157 if (!TM) 158 return; 159 160 LLT S64 = LLT::scalar(64); 161 SmallVector<Register, 4> Copies; 162 collectCopies(Copies, MF); 163 164 // Make sure DstOp version works. sqrt is just a placeholder intrinsic. 165 B.buildIntrinsic(Intrinsic::sqrt, {S64}, false) 166 .addUse(Copies[0]); 167 168 // Make sure register version works 169 SmallVector<Register, 1> Results; 170 Results.push_back(MRI->createGenericVirtualRegister(S64)); 171 B.buildIntrinsic(Intrinsic::sqrt, Results, false) 172 .addUse(Copies[1]); 173 174 auto CheckStr = R"( 175 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 176 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 177 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_ 178 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_ 179 )"; 180 181 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 182 } 183 184 TEST_F(GISelMITest, BuildXor) { 185 if (!TM) 186 return; 187 188 LLT S64 = LLT::scalar(64); 189 LLT S128 = LLT::scalar(128); 190 SmallVector<Register, 4> Copies; 191 collectCopies(Copies, MF); 192 B.buildXor(S64, Copies[0], Copies[1]); 193 B.buildNot(S64, Copies[0]); 194 195 // Make sure this works with > 64-bit types 196 auto Merge = B.buildMerge(S128, {Copies[0], Copies[1]}); 197 B.buildNot(S128, Merge); 198 auto CheckStr = R"( 199 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 200 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 201 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_ 202 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1 203 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_ 204 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64) 205 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1 206 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_ 207 )"; 208 209 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 210 } 211 212 TEST_F(GISelMITest, BuildBitCounts) { 213 if (!TM) 214 return; 215 216 LLT S32 = LLT::scalar(32); 217 SmallVector<Register, 4> Copies; 218 collectCopies(Copies, MF); 219 220 B.buildCTPOP(S32, Copies[0]); 221 B.buildCTLZ(S32, Copies[0]); 222 B.buildCTLZ_ZERO_UNDEF(S32, Copies[1]); 223 B.buildCTTZ(S32, Copies[0]); 224 B.buildCTTZ_ZERO_UNDEF(S32, Copies[1]); 225 226 auto CheckStr = R"( 227 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 228 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 229 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_ 230 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_ 231 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_ 232 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_ 233 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_ 234 )"; 235 236 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 237 } 238 239 TEST_F(GISelMITest, BuildCasts) { 240 if (!TM) 241 return; 242 243 LLT S32 = LLT::scalar(32); 244 SmallVector<Register, 4> Copies; 245 collectCopies(Copies, MF); 246 247 B.buildUITOFP(S32, Copies[0]); 248 B.buildSITOFP(S32, Copies[0]); 249 B.buildFPTOUI(S32, Copies[0]); 250 B.buildFPTOSI(S32, Copies[0]); 251 252 auto CheckStr = R"( 253 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 254 ; CHECK: [[UITOFP:%[0-9]+]]:_(s32) = G_UITOFP [[COPY0]]:_ 255 ; CHECK: [[SITOFP:%[0-9]+]]:_(s32) = G_SITOFP [[COPY0]]:_ 256 ; CHECK: [[FPTOUI:%[0-9]+]]:_(s32) = G_FPTOUI [[COPY0]]:_ 257 ; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[COPY0]]:_ 258 )"; 259 260 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 261 } 262 263 TEST_F(GISelMITest, BuildMinMax) { 264 if (!TM) 265 return; 266 267 LLT S64 = LLT::scalar(64); 268 SmallVector<Register, 4> Copies; 269 collectCopies(Copies, MF); 270 271 B.buildSMin(S64, Copies[0], Copies[1]); 272 B.buildSMax(S64, Copies[0], Copies[1]); 273 B.buildUMin(S64, Copies[0], Copies[1]); 274 B.buildUMax(S64, Copies[0], Copies[1]); 275 276 auto CheckStr = R"( 277 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 278 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 279 ; CHECK: [[SMIN0:%[0-9]+]]:_(s64) = G_SMIN [[COPY0]]:_, [[COPY1]]:_ 280 ; CHECK: [[SMAX0:%[0-9]+]]:_(s64) = G_SMAX [[COPY0]]:_, [[COPY1]]:_ 281 ; CHECK: [[UMIN0:%[0-9]+]]:_(s64) = G_UMIN [[COPY0]]:_, [[COPY1]]:_ 282 ; CHECK: [[UMAX0:%[0-9]+]]:_(s64) = G_UMAX [[COPY0]]:_, [[COPY1]]:_ 283 )"; 284 285 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 286 } 287 288 TEST_F(GISelMITest, BuildAtomicRMW) { 289 if (!TM) 290 return; 291 292 LLT S64 = LLT::scalar(64); 293 LLT P0 = LLT::pointer(0, 64); 294 SmallVector<Register, 4> Copies; 295 collectCopies(Copies, MF); 296 297 MachineMemOperand *MMO = 298 MF->getMachineMemOperand( 299 MachinePointerInfo(), 300 MachineMemOperand::MOLoad | MachineMemOperand::MOStore, 301 8, 8, AAMDNodes(), nullptr, SyncScope::System, AtomicOrdering::Unordered); 302 303 auto Ptr = B.buildUndef(P0); 304 B.buildAtomicRMWFAdd(S64, Ptr, Copies[0], *MMO); 305 B.buildAtomicRMWFSub(S64, Ptr, Copies[0], *MMO); 306 307 auto CheckStr = R"( 308 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 309 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 310 ; CHECK: [[PTR:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF 311 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_ATOMICRMW_FADD [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8) 312 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_ATOMICRMW_FSUB [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8) 313 )"; 314 315 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 316 } 317