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
.buildFNeg(S64
, Copies
[0]);
134 B
.buildFAbs(S64
, Copies
[0]);
135 B
.buildFCopysign(S64
, Copies
[0], Copies
[1]);
138 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
139 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
140 ; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2
141 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_FADD [[COPY0]]:_, [[COPY1]]:_
142 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_FSUB [[COPY0]]:_, [[COPY1]]:_
143 ; CHECK: [[FMA:%[0-9]+]]:_(s64) = G_FMA [[COPY0]]:_, [[COPY1]]:_, [[COPY2]]:_
144 ; CHECK: [[FNEG:%[0-9]+]]:_(s64) = G_FNEG [[COPY0]]:_
145 ; CHECK: [[FABS:%[0-9]+]]:_(s64) = G_FABS [[COPY0]]:_
146 ; CHECK: [[FCOPYSIGN:%[0-9]+]]:_(s64) = G_FCOPYSIGN [[COPY0]]:_, [[COPY1]]:_
149 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
152 TEST_F(GISelMITest
, BuildIntrinsic
) {
156 LLT S64
= LLT::scalar(64);
157 SmallVector
<Register
, 4> Copies
;
158 collectCopies(Copies
, MF
);
160 // Make sure DstOp version works. sqrt is just a placeholder intrinsic.
161 B
.buildIntrinsic(Intrinsic::sqrt
, {S64
}, false)
164 // Make sure register version works
165 SmallVector
<Register
, 1> Results
;
166 Results
.push_back(MRI
->createGenericVirtualRegister(S64
));
167 B
.buildIntrinsic(Intrinsic::sqrt
, Results
, false)
171 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
172 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
173 ; CHECK: [[SQRT0:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY0]]:_
174 ; CHECK: [[SQRT1:%[0-9]+]]:_(s64) = G_INTRINSIC intrinsic(@llvm.sqrt), [[COPY1]]:_
177 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
180 TEST_F(GISelMITest
, BuildXor
) {
184 LLT S64
= LLT::scalar(64);
185 LLT S128
= LLT::scalar(128);
186 SmallVector
<Register
, 4> Copies
;
187 collectCopies(Copies
, MF
);
188 B
.buildXor(S64
, Copies
[0], Copies
[1]);
189 B
.buildNot(S64
, Copies
[0]);
191 // Make sure this works with > 64-bit types
192 auto Merge
= B
.buildMerge(S128
, {Copies
[0], Copies
[1]});
193 B
.buildNot(S128
, Merge
);
195 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
196 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
197 ; CHECK: [[XOR0:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[COPY1]]:_
198 ; CHECK: [[NEGONE64:%[0-9]+]]:_(s64) = G_CONSTANT i64 -1
199 ; CHECK: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[COPY0]]:_, [[NEGONE64]]:_
200 ; CHECK: [[MERGE:%[0-9]+]]:_(s128) = G_MERGE_VALUES [[COPY0]]:_(s64), [[COPY1]]:_(s64)
201 ; CHECK: [[NEGONE128:%[0-9]+]]:_(s128) = G_CONSTANT i128 -1
202 ; CHECK: [[XOR2:%[0-9]+]]:_(s128) = G_XOR [[MERGE]]:_, [[NEGONE128]]:_
205 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
208 TEST_F(GISelMITest
, BuildBitCounts
) {
212 LLT S32
= LLT::scalar(32);
213 SmallVector
<Register
, 4> Copies
;
214 collectCopies(Copies
, MF
);
216 B
.buildCTPOP(S32
, Copies
[0]);
217 B
.buildCTLZ(S32
, Copies
[0]);
218 B
.buildCTLZ_ZERO_UNDEF(S32
, Copies
[1]);
219 B
.buildCTTZ(S32
, Copies
[0]);
220 B
.buildCTTZ_ZERO_UNDEF(S32
, Copies
[1]);
223 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
224 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
225 ; CHECK: [[CTPOP:%[0-9]+]]:_(s32) = G_CTPOP [[COPY0]]:_
226 ; CHECK: [[CTLZ0:%[0-9]+]]:_(s32) = G_CTLZ [[COPY0]]:_
227 ; CHECK: [[CTLZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTLZ_ZERO_UNDEF [[COPY1]]:_
228 ; CHECK: [[CTTZ:%[0-9]+]]:_(s32) = G_CTTZ [[COPY0]]:_
229 ; CHECK: [[CTTZ_UNDEF0:%[0-9]+]]:_(s32) = G_CTTZ_ZERO_UNDEF [[COPY1]]:_
232 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
235 TEST_F(GISelMITest
, BuildCasts
) {
239 LLT S32
= LLT::scalar(32);
240 SmallVector
<Register
, 4> Copies
;
241 collectCopies(Copies
, MF
);
243 B
.buildUITOFP(S32
, Copies
[0]);
244 B
.buildSITOFP(S32
, Copies
[0]);
245 B
.buildFPTOUI(S32
, Copies
[0]);
246 B
.buildFPTOSI(S32
, Copies
[0]);
249 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
250 ; CHECK: [[UITOFP:%[0-9]+]]:_(s32) = G_UITOFP [[COPY0]]:_
251 ; CHECK: [[SITOFP:%[0-9]+]]:_(s32) = G_SITOFP [[COPY0]]:_
252 ; CHECK: [[FPTOUI:%[0-9]+]]:_(s32) = G_FPTOUI [[COPY0]]:_
253 ; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[COPY0]]:_
256 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
259 TEST_F(GISelMITest
, BuildMinMax
) {
263 LLT S64
= LLT::scalar(64);
264 SmallVector
<Register
, 4> Copies
;
265 collectCopies(Copies
, MF
);
267 B
.buildSMin(S64
, Copies
[0], Copies
[1]);
268 B
.buildSMax(S64
, Copies
[0], Copies
[1]);
269 B
.buildUMin(S64
, Copies
[0], Copies
[1]);
270 B
.buildUMax(S64
, Copies
[0], Copies
[1]);
273 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
274 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
275 ; CHECK: [[SMIN0:%[0-9]+]]:_(s64) = G_SMIN [[COPY0]]:_, [[COPY1]]:_
276 ; CHECK: [[SMAX0:%[0-9]+]]:_(s64) = G_SMAX [[COPY0]]:_, [[COPY1]]:_
277 ; CHECK: [[UMIN0:%[0-9]+]]:_(s64) = G_UMIN [[COPY0]]:_, [[COPY1]]:_
278 ; CHECK: [[UMAX0:%[0-9]+]]:_(s64) = G_UMAX [[COPY0]]:_, [[COPY1]]:_
281 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;
284 TEST_F(GISelMITest
, BuildAtomicRMW
) {
288 LLT S64
= LLT::scalar(64);
289 LLT P0
= LLT::pointer(0, 64);
290 SmallVector
<Register
, 4> Copies
;
291 collectCopies(Copies
, MF
);
293 MachineMemOperand
*MMO
=
294 MF
->getMachineMemOperand(
295 MachinePointerInfo(),
296 MachineMemOperand::MOLoad
| MachineMemOperand::MOStore
,
297 8, 8, AAMDNodes(), nullptr, SyncScope::System
, AtomicOrdering::Unordered
);
299 auto Ptr
= B
.buildUndef(P0
);
300 B
.buildAtomicRMWFAdd(S64
, Ptr
, Copies
[0], *MMO
);
301 B
.buildAtomicRMWFSub(S64
, Ptr
, Copies
[0], *MMO
);
304 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
305 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
306 ; CHECK: [[PTR:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF
307 ; CHECK: [[FADD:%[0-9]+]]:_(s64) = G_ATOMICRMW_FADD [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
308 ; CHECK: [[FSUB:%[0-9]+]]:_(s64) = G_ATOMICRMW_FSUB [[PTR]]:_(p0), [[COPY0]]:_ :: (load store unordered 8)
311 EXPECT_TRUE(CheckMachineFunction(*MF
, CheckStr
)) << *MF
;