xref: /llvm-project/llvm/unittests/CodeGen/GlobalISel/MachineIRBuilderTest.cpp (revision f3a46d0ae977655691318e740ba9c2e8875d911c)
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