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
TEST_F(GISelMITest,TestBuildConstantFConstant)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
TEST_F(GISelMITest,TestBuildConstantFConstantDeath)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
TEST_F(GISelMITest,DstOpSrcOp)76 TEST_F(GISelMITest, DstOpSrcOp) {
77 setUp();
78 if (!TM)
79 return;
80
81 SmallVector<Register, 4> Copies;
82 collectCopies(Copies, MF);
83
84 LLT s64 = LLT::scalar(64);
85 auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
86
87 // Test SrcOp and DstOp can be constructed directly from MachineOperand by
88 // copying the instruction
89 B.buildAdd(MIBAdd->getOperand(0), MIBAdd->getOperand(1), MIBAdd->getOperand(2));
90
91
92 auto CheckStr = R"(
93 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
94 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
95 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
96 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
97 )";
98
99 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
100 }
101
TEST_F(GISelMITest,BuildUnmerge)102 TEST_F(GISelMITest, BuildUnmerge) {
103 setUp();
104 if (!TM)
105 return;
106
107 SmallVector<Register, 4> Copies;
108 collectCopies(Copies, MF);
109 B.buildUnmerge(LLT::scalar(32), Copies[0]);
110 B.buildUnmerge(LLT::scalar(16), Copies[1]);
111
112 auto CheckStr = R"(
113 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
114 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
115 ; CHECK: [[UNMERGE32_0:%[0-9]+]]:_(s32), [[UNMERGE32_1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[COPY0]]
116 ; 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]]
117
118 )";
119
120 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
121 }
122
TEST_F(GISelMITest,TestBuildFPInsts)123 TEST_F(GISelMITest, TestBuildFPInsts) {
124 setUp();
125 if (!TM)
126 return;
127
128 SmallVector<Register, 4> Copies;
129 collectCopies(Copies, MF);
130
131 LLT S64 = LLT::scalar(64);
132
133 B.buildFAdd(S64, Copies[0], Copies[1]);
134 B.buildFSub(S64, Copies[0], Copies[1]);
135 B.buildFMA(S64, Copies[0], Copies[1], Copies[2]);
136 B.buildFMAD(S64, Copies[0], Copies[1], Copies[2]);
137 B.buildFMAD(S64, Copies[0], Copies[1], Copies[2], MachineInstr::FmNoNans);
138 B.buildFNeg(S64, Copies[0]);
139 B.buildFAbs(S64, Copies[0]);
140 B.buildFCopysign(S64, Copies[0], Copies[1]);
141
142 auto CheckStr = R"(
143 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
144 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
145 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2
146 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_
147 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_
148 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
149 ; CHECK: [[FMAD0:%[0-9]+]]:_(s64) = G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
150 ; CHECK: [[FMAD1:%[0-9]+]]:_(s64) = nnan G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
151 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_
152 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_
153 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_
154 )";
155
156 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
157 }
158
TEST_F(GISelMITest,BuildIntrinsic)159 TEST_F(GISelMITest, BuildIntrinsic) {
160 setUp();
161 if (!TM)
162 return;
163
164 LLT S64 = LLT::scalar(64);
165 SmallVector<Register, 4> Copies;
166 collectCopies(Copies, MF);
167
168 // Make sure DstOp version works. sqrt is just a placeholder intrinsic.
169 B.buildIntrinsic(Intrinsic::sqrt, {S64}, false)
170 .addUse(Copies[0]);
171
172 // Make sure register version works
173 SmallVector<Register, 1> Results;
174 Results.push_back(MRI->createGenericVirtualRegister(S64));
175 B.buildIntrinsic(Intrinsic::sqrt, Results, false)
176 .addUse(Copies[1]);
177
178 auto CheckStr = R"(
179 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
180 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
181 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_
182 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_
183 )";
184
185 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
186 }
187
TEST_F(GISelMITest,BuildXor)188 TEST_F(GISelMITest, BuildXor) {
189 setUp();
190 if (!TM)
191 return;
192
193 LLT S64 = LLT::scalar(64);
194 LLT S128 = LLT::scalar(128);
195 SmallVector<Register, 4> Copies;
196 collectCopies(Copies, MF);
197 B.buildXor(S64, Copies[0], Copies[1]);
198 B.buildNot(S64, Copies[0]);
199
200 // Make sure this works with > 64-bit types
201 auto Merge = B.buildMerge(S128, {Copies[0], Copies[1]});
202 B.buildNot(S128, Merge);
203 auto CheckStr = R"(
204 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
205 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
206 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_
207 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1
208 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_
209 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64)
210 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1
211 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_
212 )";
213
214 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
215 }
216
TEST_F(GISelMITest,BuildBitCounts)217 TEST_F(GISelMITest, BuildBitCounts) {
218 setUp();
219 if (!TM)
220 return;
221
222 LLT S32 = LLT::scalar(32);
223 SmallVector<Register, 4> Copies;
224 collectCopies(Copies, MF);
225
226 B.buildCTPOP(S32, Copies[0]);
227 B.buildCTLZ(S32, Copies[0]);
228 B.buildCTLZ_ZERO_UNDEF(S32, Copies[1]);
229 B.buildCTTZ(S32, Copies[0]);
230 B.buildCTTZ_ZERO_UNDEF(S32, Copies[1]);
231
232 auto CheckStr = R"(
233 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
234 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
235 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_
236 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_
237 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_
238 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_
239 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_
240 )";
241
242 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
243 }
244
TEST_F(GISelMITest,BuildCasts)245 TEST_F(GISelMITest, BuildCasts) {
246 setUp();
247 if (!TM)
248 return;
249
250 LLT S32 = LLT::scalar(32);
251 SmallVector<Register, 4> Copies;
252 collectCopies(Copies, MF);
253
254 B.buildUITOFP(S32, Copies[0]);
255 B.buildSITOFP(S32, Copies[0]);
256 B.buildFPTOUI(S32, Copies[0]);
257 B.buildFPTOSI(S32, Copies[0]);
258
259 auto CheckStr = R"(
260 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
261 ; CHECK: [[UITOFP:%[0-9]+]]:_(s32) = G_UITOFP [[COPY0]]:_
262 ; CHECK: [[SITOFP:%[0-9]+]]:_(s32) = G_SITOFP [[COPY0]]:_
263 ; CHECK: [[FPTOUI:%[0-9]+]]:_(s32) = G_FPTOUI [[COPY0]]:_
264 ; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[COPY0]]:_
265 )";
266
267 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
268 }
269
TEST_F(GISelMITest,BuildMinMax)270 TEST_F(GISelMITest, BuildMinMax) {
271 setUp();
272 if (!TM)
273 return;
274
275 LLT S64 = LLT::scalar(64);
276 SmallVector<Register, 4> Copies;
277 collectCopies(Copies, MF);
278
279 B.buildSMin(S64, Copies[0], Copies[1]);
280 B.buildSMax(S64, Copies[0], Copies[1]);
281 B.buildUMin(S64, Copies[0], Copies[1]);
282 B.buildUMax(S64, Copies[0], Copies[1]);
283
284 auto CheckStr = R"(
285 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
286 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
287 ; CHECK: [[SMIN0:%[0-9]+]]:_(s64) = G_SMIN [[COPY0]]:_, [[COPY1]]:_
288 ; CHECK: [[SMAX0:%[0-9]+]]:_(s64) = G_SMAX [[COPY0]]:_, [[COPY1]]:_
289 ; CHECK: [[UMIN0:%[0-9]+]]:_(s64) = G_UMIN [[COPY0]]:_, [[COPY1]]:_
290 ; CHECK: [[UMAX0:%[0-9]+]]:_(s64) = G_UMAX [[COPY0]]:_, [[COPY1]]:_
291 )";
292
293 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
294 }
295
TEST_F(GISelMITest,BuildAtomicRMW)296 TEST_F(GISelMITest, BuildAtomicRMW) {
297 setUp();
298 if (!TM)
299 return;
300
301 LLT S64 = LLT::scalar(64);
302 LLT P0 = LLT::pointer(0, 64);
303 SmallVector<Register, 4> Copies;
304 collectCopies(Copies, MF);
305
306 MachineMemOperand *MMO =
307 MF->getMachineMemOperand(
308 MachinePointerInfo(),
309 MachineMemOperand::MOLoad | MachineMemOperand::MOStore,
310 8, 8, AAMDNodes(), nullptr, SyncScope::System, AtomicOrdering::Unordered);
311
312 auto Ptr = B.buildUndef(P0);
313 B.buildAtomicRMWFAdd(S64, Ptr, Copies[0], *MMO);
314 B.buildAtomicRMWFSub(S64, Ptr, Copies[0], *MMO);
315
316 auto CheckStr = R"(
317 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
318 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
319 ; CHECK: [[PTR:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF
320 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_ATOMICRMW_FADD [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
321 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_ATOMICRMW_FSUB [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
322 )";
323
324 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
325 }
326
TEST_F(GISelMITest,BuildMerge)327 TEST_F(GISelMITest, BuildMerge) {
328 setUp();
329 if (!TM)
330 return;
331
332 LLT S32 = LLT::scalar(32);
333 Register RegC0 = B.buildConstant(S32, 0)->getOperand(0).getReg();
334 Register RegC1 = B.buildConstant(S32, 1)->getOperand(0).getReg();
335 Register RegC2 = B.buildConstant(S32, 2)->getOperand(0).getReg();
336 Register RegC3 = B.buildConstant(S32, 3)->getOperand(0).getReg();
337
338 // Merging plain constants as one big blob of bit should produce a
339 // G_MERGE_VALUES.
340 B.buildMerge(LLT::scalar(128), {RegC0, RegC1, RegC2, RegC3});
341 // Merging plain constants to a vector should produce a G_BUILD_VECTOR.
342 LLT V2x32 = LLT::vector(2, 32);
343 Register RegC0C1 =
344 B.buildMerge(V2x32, {RegC0, RegC1})->getOperand(0).getReg();
345 Register RegC2C3 =
346 B.buildMerge(V2x32, {RegC2, RegC3})->getOperand(0).getReg();
347 // Merging vector constants to a vector should produce a G_CONCAT_VECTORS.
348 B.buildMerge(LLT::vector(4, 32), {RegC0C1, RegC2C3});
349 // Merging vector constants to a plain type is not allowed.
350 // Nothing else to test.
351
352 auto CheckStr = R"(
353 ; CHECK: [[C0:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
354 ; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
355 ; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 2
356 ; CHECK: [[C3:%[0-9]+]]:_(s32) = G_CONSTANT i32 3
357 ; CHECK: {{%[0-9]+}}:_(s128) = G_MERGE_VALUES [[C0]]:_(s32), [[C1]]:_(s32), [[C2]]:_(s32), [[C3]]:_(s32)
358 ; CHECK: [[LOW2x32:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[C0]]:_(s32), [[C1]]:_(s32)
359 ; CHECK: [[HIGH2x32:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[C2]]:_(s32), [[C3]]:_(s32)
360 ; CHECK: {{%[0-9]+}}:_(<4 x s32>) = G_CONCAT_VECTORS [[LOW2x32]]:_(<2 x s32>), [[HIGH2x32]]:_(<2 x s32>)
361 )";
362
363 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
364 }
365