1 //===- MachineIRBuilderTest.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "GISelMITest.h"
10 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
12 TEST_F(GISelMITest
, TestBuildConstantFConstant
) {
17 B
.buildConstant(LLT::scalar(32), 42);
18 B
.buildFConstant(LLT::scalar(32), 1.0);
20 B
.buildConstant(LLT::vector(2, 32), 99);
21 B
.buildFConstant(LLT::vector(2, 32), 2.0);
23 // Test APFloat overload.
24 APFloat
KVal(APFloat::IEEEdouble(), "4.0");
25 B
.buildFConstant(LLT::scalar(64), KVal
);
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
37 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
41 #ifdef GTEST_HAS_DEATH_TEST
44 TEST_F(GISelMITest
, TestBuildConstantFConstantDeath
) {
49 LLVMContext
&Ctx
= MF
->getFunction().getContext();
50 APInt
APV32(32, 12345);
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");
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");
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");
76 TEST_F(GISelMITest
, DstOpSrcOp
) {
81 SmallVector
<Register
, 4> Copies
;
82 collectCopies(Copies
, MF
);
84 LLT s64
= LLT::scalar(64);
85 auto MIBAdd
= B
.buildAdd(s64
, Copies
[0], Copies
[1]);
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));
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]]:_
99 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
102 TEST_F(GISelMITest
, BuildUnmerge
) {
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]);
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]]
120 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
123 TEST_F(GISelMITest
, TestBuildFPInsts
) {
128 SmallVector
<Register
, 4> Copies
;
129 collectCopies(Copies
, MF
);
131 LLT S64
= LLT::scalar(64);
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]);
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]]:_
156 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
159 TEST_F(GISelMITest
, BuildIntrinsic
) {
164 LLT S64
= LLT::scalar(64);
165 SmallVector
<Register
, 4> Copies
;
166 collectCopies(Copies
, MF
);
168 // Make sure DstOp version works. sqrt is just a placeholder intrinsic.
169 B
.buildIntrinsic(Intrinsic::sqrt
, {S64
}, false)
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)
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]]:_
185 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
188 TEST_F(GISelMITest
, BuildXor
) {
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]);
200 // Make sure this works with > 64-bit types
201 auto Merge
= B
.buildMerge(S128
, {Copies
[0], Copies
[1]});
202 B
.buildNot(S128
, Merge
);
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]]:_
214 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
217 TEST_F(GISelMITest
, BuildBitCounts
) {
222 LLT S32
= LLT::scalar(32);
223 SmallVector
<Register
, 4> Copies
;
224 collectCopies(Copies
, MF
);
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]);
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]]:_
242 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
245 TEST_F(GISelMITest
, BuildCasts
) {
250 LLT S32
= LLT::scalar(32);
251 SmallVector
<Register
, 4> Copies
;
252 collectCopies(Copies
, MF
);
254 B
.buildUITOFP(S32
, Copies
[0]);
255 B
.buildSITOFP(S32
, Copies
[0]);
256 B
.buildFPTOUI(S32
, Copies
[0]);
257 B
.buildFPTOSI(S32
, Copies
[0]);
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]]:_
267 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
270 TEST_F(GISelMITest
, BuildMinMax
) {
275 LLT S64
= LLT::scalar(64);
276 SmallVector
<Register
, 4> Copies
;
277 collectCopies(Copies
, MF
);
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]);
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]]:_
293 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
296 TEST_F(GISelMITest
, BuildAtomicRMW
) {
301 LLT S64
= LLT::scalar(64);
302 LLT P0
= LLT::pointer(0, 64);
303 SmallVector
<Register
, 4> Copies
;
304 collectCopies(Copies
, MF
);
306 MachineMemOperand
*MMO
=
307 MF
->getMachineMemOperand(
308 MachinePointerInfo(),
309 MachineMemOperand::MOLoad
| MachineMemOperand::MOStore
,
310 8, 8, AAMDNodes(), nullptr, SyncScope::System
, AtomicOrdering::Unordered
);
312 auto Ptr
= B
.buildUndef(P0
);
313 B
.buildAtomicRMWFAdd(S64
, Ptr
, Copies
[0], *MMO
);
314 B
.buildAtomicRMWFSub(S64
, Ptr
, Copies
[0], *MMO
);
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)
324 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
327 TEST_F(GISelMITest
, BuildMerge
) {
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();
338 // Merging plain constants as one big blob of bit should produce a
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);
344 B
.buildMerge(V2x32
, {RegC0
, RegC1
})->getOperand(0).getReg();
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.
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>)
363 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;