xref: /llvm-project/llvm/unittests/CodeGen/GlobalISel/MachineIRBuilderTest.cpp (revision d72f25e5b00511081dbd4f1a80bcaaa098ce7873)
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 
147 TEST_F(GISelMITest, BuildIntrinsic) {
148   if (!TM)
149     return;
150 
151   LLT S64 = LLT::scalar(64);
152   SmallVector<unsigned, 4> Copies;
153   collectCopies(Copies, MF);
154 
155   // Make sure DstOp version works. sqrt is just a placeholder intrinsic.
156   B.buildIntrinsic(Intrinsic::sqrt, {S64}, false)
157     .addUse(Copies[0]);
158 
159   // Make sure register version works
160   SmallVector<unsigned, 1> Results;
161   Results.push_back(MRI->createGenericVirtualRegister(S64));
162   B.buildIntrinsic(Intrinsic::sqrt, Results, false)
163     .addUse(Copies[1]);
164 
165   auto CheckStr = R"(
166   ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
167   ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
168   ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_
169   ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_
170   )";
171 
172   EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
173 }
174 
175 TEST_F(GISelMITest, BuildXor) {
176   if (!TM)
177     return;
178 
179   LLT S64 = LLT::scalar(64);
180   LLT S128 = LLT::scalar(128);
181   SmallVector<unsigned, 4> Copies;
182   collectCopies(Copies, MF);
183   B.buildXor(S64, Copies[0], Copies[1]);
184   B.buildNot(S64, Copies[0]);
185 
186   // Make sure this works with > 64-bit types
187   auto Merge = B.buildMerge(S128, {Copies[0], Copies[1]});
188   B.buildNot(S128, Merge);
189   auto CheckStr = R"(
190   ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
191   ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
192   ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_
193   ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1
194   ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_
195   ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64)
196   ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1
197   ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_
198   )";
199 
200   EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
201 }
202