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 auto CheckStr = R"( 23 CHECK: [[CONST0:%[0-9]+]]:_(s32) = G_CONSTANT i32 42 24 CHECK: [[FCONST0:%[0-9]+]]:_(s32) = G_FCONSTANT float 1.000000e+00 25 CHECK: [[CONST1:%[0-9]+]]:_(s32) = G_CONSTANT i32 99 26 CHECK: [[VEC0:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[CONST1]]:_(s32), [[CONST1]]:_(s32) 27 CHECK: [[FCONST1:%[0-9]+]]:_(s32) = G_FCONSTANT float 2.000000e+00 28 CHECK: [[VEC1:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[FCONST1]]:_(s32), [[FCONST1]]:_(s32) 29 )"; 30 31 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 32 } 33 34 35 #ifdef GTEST_HAS_DEATH_TEST 36 #ifndef NDEBUG 37 38 TEST_F(GISelMITest, TestBuildConstantFConstantDeath) { 39 if (!TM) 40 return; 41 42 LLVMContext &Ctx = MF->getFunction().getContext(); 43 APInt APV32(32, 12345); 44 45 // Test APInt version breaks 46 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), APV32), 47 "creating constant with the wrong size"); 48 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), APV32), 49 "creating constant with the wrong size"); 50 51 // Test ConstantInt version breaks 52 ConstantInt *CI = ConstantInt::get(Ctx, APV32); 53 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), *CI), 54 "creating constant with the wrong size"); 55 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), *CI), 56 "creating constant with the wrong size"); 57 58 APFloat DoubleVal(APFloat::IEEEdouble()); 59 ConstantFP *CF = ConstantFP::get(Ctx, DoubleVal); 60 EXPECT_DEATH(B.buildFConstant(LLT::scalar(16), *CF), 61 "creating fconstant with the wrong size"); 62 EXPECT_DEATH(B.buildFConstant(LLT::vector(2, 16), *CF), 63 "creating fconstant with the wrong size"); 64 } 65 66 #endif 67 #endif 68 69 TEST_F(GISelMITest, DstOpSrcOp) { 70 if (!TM) 71 return; 72 73 SmallVector<unsigned, 4> Copies; 74 collectCopies(Copies, MF); 75 76 LLT s64 = LLT::scalar(64); 77 auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]); 78 79 // Test SrcOp and DstOp can be constructed directly from MachineOperand by 80 // copying the instruction 81 B.buildAdd(MIBAdd->getOperand(0), MIBAdd->getOperand(1), MIBAdd->getOperand(2)); 82 83 84 auto CheckStr = R"( 85 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 86 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 87 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 88 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_ 89 )"; 90 91 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 92 } 93 94 TEST_F(GISelMITest, BuildUnmerge) { 95 if (!TM) 96 return; 97 98 SmallVector<unsigned, 4> Copies; 99 collectCopies(Copies, MF); 100 B.buildUnmerge(LLT::scalar(32), Copies[0]); 101 B.buildUnmerge(LLT::scalar(16), Copies[1]); 102 103 auto CheckStr = R"( 104 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 105 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 106 ; CHECK: [[UNMERGE32_0:%[0-9]+]]:_(s32), [[UNMERGE32_1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[COPY0]] 107 ; 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]] 108 109 )"; 110 111 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 112 } 113 114 TEST_F(GISelMITest, TestBuildFPInsts) { 115 if (!TM) 116 return; 117 118 SmallVector<unsigned, 4> Copies; 119 collectCopies(Copies, MF); 120 121 LLT S64 = LLT::scalar(64); 122 123 B.buildFAdd(S64, Copies[0], Copies[1]); 124 B.buildFSub(S64, Copies[0], Copies[1]); 125 B.buildFNeg(S64, Copies[0]); 126 B.buildFAbs(S64, Copies[0]); 127 B.buildFCopysign(S64, Copies[0], Copies[1]); 128 129 auto CheckStr = R"( 130 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0 131 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1 132 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_ 133 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_ 134 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_ 135 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_ 136 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_ 137 )"; 138 139 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF; 140 } 141