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
) {
80 SmallVector
<Register
, 4> Copies
;
81 collectCopies(Copies
, MF
);
83 LLT s64
= LLT::scalar(64);
84 auto MIBAdd
= B
.buildAdd(s64
, Copies
[0], Copies
[1]);
86 // Test SrcOp and DstOp can be constructed directly from MachineOperand by
87 // copying the instruction
88 B
.buildAdd(MIBAdd
->getOperand(0), MIBAdd
->getOperand(1), MIBAdd
->getOperand(2));
92 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
93 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
94 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
95 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
98 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
101 TEST_F(GISelMITest
, BuildUnmerge
) {
105 SmallVector
<Register
, 4> Copies
;
106 collectCopies(Copies
, MF
);
107 B
.buildUnmerge(LLT::scalar(32), Copies
[0]);
108 B
.buildUnmerge(LLT::scalar(16), Copies
[1]);
111 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
112 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
113 ; CHECK: [[UNMERGE32_0:%[0-9]+]]:_(s32), [[UNMERGE32_1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[COPY0]]
114 ; 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]]
118 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
121 TEST_F(GISelMITest
, TestBuildFPInsts
) {
125 SmallVector
<Register
, 4> Copies
;
126 collectCopies(Copies
, MF
);
128 LLT S64
= LLT::scalar(64);
130 B
.buildFAdd(S64
, Copies
[0], Copies
[1]);
131 B
.buildFSub(S64
, Copies
[0], Copies
[1]);
132 B
.buildFMA(S64
, Copies
[0], Copies
[1], Copies
[2]);
133 B
.buildFMAD(S64
, Copies
[0], Copies
[1], Copies
[2]);
134 B
.buildFMAD(S64
, Copies
[0], Copies
[1], Copies
[2], MachineInstr::FmNoNans
);
135 B
.buildFNeg(S64
, Copies
[0]);
136 B
.buildFAbs(S64
, Copies
[0]);
137 B
.buildFCopysign(S64
, Copies
[0], Copies
[1]);
140 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
141 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
142 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2
143 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_
144 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_
145 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
146 ; CHECK: [[FMAD0:%[0-9]+]]:_(s64) = G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
147 ; CHECK: [[FMAD1:%[0-9]+]]:_(s64) = nnan G_FMAD [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
148 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_
149 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_
150 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_
153 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
156 TEST_F(GISelMITest
, BuildIntrinsic
) {
160 LLT S64
= LLT::scalar(64);
161 SmallVector
<Register
, 4> Copies
;
162 collectCopies(Copies
, MF
);
164 // Make sure DstOp version works. sqrt is just a placeholder intrinsic.
165 B
.buildIntrinsic(Intrinsic::sqrt
, {S64
}, false)
168 // Make sure register version works
169 SmallVector
<Register
, 1> Results
;
170 Results
.push_back(MRI
->createGenericVirtualRegister(S64
));
171 B
.buildIntrinsic(Intrinsic::sqrt
, Results
, false)
175 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
176 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
177 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_
178 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_
181 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
184 TEST_F(GISelMITest
, BuildXor
) {
188 LLT S64
= LLT::scalar(64);
189 LLT S128
= LLT::scalar(128);
190 SmallVector
<Register
, 4> Copies
;
191 collectCopies(Copies
, MF
);
192 B
.buildXor(S64
, Copies
[0], Copies
[1]);
193 B
.buildNot(S64
, Copies
[0]);
195 // Make sure this works with > 64-bit types
196 auto Merge
= B
.buildMerge(S128
, {Copies
[0], Copies
[1]});
197 B
.buildNot(S128
, Merge
);
199 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
200 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
201 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_
202 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1
203 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_
204 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64)
205 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1
206 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_
209 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
212 TEST_F(GISelMITest
, BuildBitCounts
) {
216 LLT S32
= LLT::scalar(32);
217 SmallVector
<Register
, 4> Copies
;
218 collectCopies(Copies
, MF
);
220 B
.buildCTPOP(S32
, Copies
[0]);
221 B
.buildCTLZ(S32
, Copies
[0]);
222 B
.buildCTLZ_ZERO_UNDEF(S32
, Copies
[1]);
223 B
.buildCTTZ(S32
, Copies
[0]);
224 B
.buildCTTZ_ZERO_UNDEF(S32
, Copies
[1]);
227 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
228 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
229 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_
230 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_
231 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_
232 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_
233 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_
236 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
239 TEST_F(GISelMITest
, BuildCasts
) {
243 LLT S32
= LLT::scalar(32);
244 SmallVector
<Register
, 4> Copies
;
245 collectCopies(Copies
, MF
);
247 B
.buildUITOFP(S32
, Copies
[0]);
248 B
.buildSITOFP(S32
, Copies
[0]);
249 B
.buildFPTOUI(S32
, Copies
[0]);
250 B
.buildFPTOSI(S32
, Copies
[0]);
253 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
254 ; CHECK: [[UITOFP:%[0-9]+]]:_(s32) = G_UITOFP [[COPY0]]:_
255 ; CHECK: [[SITOFP:%[0-9]+]]:_(s32) = G_SITOFP [[COPY0]]:_
256 ; CHECK: [[FPTOUI:%[0-9]+]]:_(s32) = G_FPTOUI [[COPY0]]:_
257 ; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[COPY0]]:_
260 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
263 TEST_F(GISelMITest
, BuildMinMax
) {
267 LLT S64
= LLT::scalar(64);
268 SmallVector
<Register
, 4> Copies
;
269 collectCopies(Copies
, MF
);
271 B
.buildSMin(S64
, Copies
[0], Copies
[1]);
272 B
.buildSMax(S64
, Copies
[0], Copies
[1]);
273 B
.buildUMin(S64
, Copies
[0], Copies
[1]);
274 B
.buildUMax(S64
, Copies
[0], Copies
[1]);
277 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
278 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
279 ; CHECK: [[SMIN0:%[0-9]+]]:_(s64) = G_SMIN [[COPY0]]:_, [[COPY1]]:_
280 ; CHECK: [[SMAX0:%[0-9]+]]:_(s64) = G_SMAX [[COPY0]]:_, [[COPY1]]:_
281 ; CHECK: [[UMIN0:%[0-9]+]]:_(s64) = G_UMIN [[COPY0]]:_, [[COPY1]]:_
282 ; CHECK: [[UMAX0:%[0-9]+]]:_(s64) = G_UMAX [[COPY0]]:_, [[COPY1]]:_
285 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
288 TEST_F(GISelMITest
, BuildAtomicRMW
) {
292 LLT S64
= LLT::scalar(64);
293 LLT P0
= LLT::pointer(0, 64);
294 SmallVector
<Register
, 4> Copies
;
295 collectCopies(Copies
, MF
);
297 MachineMemOperand
*MMO
=
298 MF
->getMachineMemOperand(
299 MachinePointerInfo(),
300 MachineMemOperand::MOLoad
| MachineMemOperand::MOStore
,
301 8, 8, AAMDNodes(), nullptr, SyncScope::System
, AtomicOrdering::Unordered
);
303 auto Ptr
= B
.buildUndef(P0
);
304 B
.buildAtomicRMWFAdd(S64
, Ptr
, Copies
[0], *MMO
);
305 B
.buildAtomicRMWFSub(S64
, Ptr
, Copies
[0], *MMO
);
308 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
309 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
310 ; CHECK: [[PTR:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF
311 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_ATOMICRMW_FADD [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
312 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_ATOMICRMW_FSUB [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
315 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;