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.buildFNeg(S64, Copies[0]); 131 B.buildFAbs(S64, Copies[0]); 132 B.buildFCopysign(S64, Copies[0], Copies[1]); 133 134 auto CheckStr = R"( 135 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 136 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 137 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_ 138 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_ 139 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_ 140 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_ 141 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_ 142 )"; 143 144 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 145 } 146