1ff384481SMarcello Maggioni //===- ConstantFoldingTest.cpp -------------------------------------------===//
2ff384481SMarcello Maggioni //
3ff384481SMarcello Maggioni // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ff384481SMarcello Maggioni // See https://llvm.org/LICENSE.txt for license information.
5ff384481SMarcello Maggioni // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ff384481SMarcello Maggioni //
7ff384481SMarcello Maggioni //===----------------------------------------------------------------------===//
8ff384481SMarcello Maggioni
9ff384481SMarcello Maggioni #include "GISelMITest.h"
1063af3c00SJay Foad #include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
11ff384481SMarcello Maggioni #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
12ff384481SMarcello Maggioni #include "llvm/CodeGen/GlobalISel/Utils.h"
13ff384481SMarcello Maggioni #include "llvm/CodeGen/MachineFunction.h"
14ff384481SMarcello Maggioni #include "gtest/gtest.h"
15ff384481SMarcello Maggioni
16ff384481SMarcello Maggioni using namespace llvm;
17ff384481SMarcello Maggioni
18ff384481SMarcello Maggioni namespace {
19ff384481SMarcello Maggioni
TEST_F(AArch64GISelMITest,FoldWithBuilder)2058f843a5SMatt Arsenault TEST_F(AArch64GISelMITest, FoldWithBuilder) {
2142a84d22SDaniel Sanders setUp();
22ff384481SMarcello Maggioni if (!TM)
23*7fc87159SPaul Robinson GTEST_SKIP();
24ff384481SMarcello Maggioni // Try to use the FoldableInstructionsBuilder to build binary ops.
2563af3c00SJay Foad CSEMIRBuilder CFB(B.getState());
26ff384481SMarcello Maggioni LLT s32 = LLT::scalar(32);
27ff384481SMarcello Maggioni int64_t Cst;
28ff384481SMarcello Maggioni auto MIBCAdd =
29ff384481SMarcello Maggioni CFB.buildAdd(s32, CFB.buildConstant(s32, 0), CFB.buildConstant(s32, 1));
30ff384481SMarcello Maggioni // This should be a constant now.
31b482e1bfSJay Foad bool match = mi_match(MIBCAdd.getReg(0), *MRI, m_ICst(Cst));
32ff384481SMarcello Maggioni EXPECT_TRUE(match);
33ff384481SMarcello Maggioni EXPECT_EQ(Cst, 1);
34ff384481SMarcello Maggioni auto MIBCAdd1 =
35ff384481SMarcello Maggioni CFB.buildInstr(TargetOpcode::G_ADD, {s32},
36ff384481SMarcello Maggioni {CFB.buildConstant(s32, 0), CFB.buildConstant(s32, 1)});
37ff384481SMarcello Maggioni // This should be a constant now.
38b482e1bfSJay Foad match = mi_match(MIBCAdd1.getReg(0), *MRI, m_ICst(Cst));
39ff384481SMarcello Maggioni EXPECT_TRUE(match);
40ff384481SMarcello Maggioni EXPECT_EQ(Cst, 1);
41ff384481SMarcello Maggioni
42ff384481SMarcello Maggioni // Try one of the other constructors of MachineIRBuilder to make sure it's
43ff384481SMarcello Maggioni // compatible.
4463af3c00SJay Foad CSEMIRBuilder CFB1(*MF);
45ff384481SMarcello Maggioni CFB1.setInsertPt(*EntryMBB, EntryMBB->end());
46ff384481SMarcello Maggioni auto MIBCSub =
47ff384481SMarcello Maggioni CFB1.buildInstr(TargetOpcode::G_SUB, {s32},
48ff384481SMarcello Maggioni {CFB1.buildConstant(s32, 1), CFB1.buildConstant(s32, 1)});
49ff384481SMarcello Maggioni // This should be a constant now.
50b482e1bfSJay Foad match = mi_match(MIBCSub.getReg(0), *MRI, m_ICst(Cst));
51ff384481SMarcello Maggioni EXPECT_TRUE(match);
52ff384481SMarcello Maggioni EXPECT_EQ(Cst, 0);
53ff384481SMarcello Maggioni
54ff384481SMarcello Maggioni auto MIBCSext1 =
55ff384481SMarcello Maggioni CFB1.buildInstr(TargetOpcode::G_SEXT_INREG, {s32},
56ff384481SMarcello Maggioni {CFB1.buildConstant(s32, 0x01), uint64_t(8)});
57ff384481SMarcello Maggioni // This should be a constant now.
58b482e1bfSJay Foad match = mi_match(MIBCSext1.getReg(0), *MRI, m_ICst(Cst));
59ff384481SMarcello Maggioni EXPECT_TRUE(match);
60ff384481SMarcello Maggioni EXPECT_EQ(1, Cst);
61ff384481SMarcello Maggioni
62ff384481SMarcello Maggioni auto MIBCSext2 =
63ff384481SMarcello Maggioni CFB1.buildInstr(TargetOpcode::G_SEXT_INREG, {s32},
64ff384481SMarcello Maggioni {CFB1.buildConstant(s32, 0x80), uint64_t(8)});
65ff384481SMarcello Maggioni // This should be a constant now.
66b482e1bfSJay Foad match = mi_match(MIBCSext2.getReg(0), *MRI, m_ICst(Cst));
67ff384481SMarcello Maggioni EXPECT_TRUE(match);
68ff384481SMarcello Maggioni EXPECT_EQ(-0x80, Cst);
69ff384481SMarcello Maggioni }
70ff384481SMarcello Maggioni
TEST_F(AArch64GISelMITest,FoldBinOp)7158f843a5SMatt Arsenault TEST_F(AArch64GISelMITest, FoldBinOp) {
7242a84d22SDaniel Sanders setUp();
730112123eSMarcello Maggioni if (!TM)
74*7fc87159SPaul Robinson GTEST_SKIP();
750112123eSMarcello Maggioni
760112123eSMarcello Maggioni LLT s32{LLT::scalar(32)};
770112123eSMarcello Maggioni auto MIBCst1 = B.buildConstant(s32, 16);
780112123eSMarcello Maggioni auto MIBCst2 = B.buildConstant(s32, 9);
790112123eSMarcello Maggioni auto MIBFCst1 = B.buildFConstant(s32, 1.0000001);
800112123eSMarcello Maggioni auto MIBFCst2 = B.buildFConstant(s32, 2.0);
810112123eSMarcello Maggioni
820112123eSMarcello Maggioni // Test G_ADD folding Integer + Mixed Int-Float cases
8367819a72SFangrui Song std::optional<APInt> FoldGAddInt = ConstantFoldBinOp(
8467819a72SFangrui Song TargetOpcode::G_ADD, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
853361a364SKazu Hirata EXPECT_TRUE(FoldGAddInt.has_value());
8667ba5c50SFangrui Song EXPECT_EQ(25ULL, FoldGAddInt->getLimitedValue());
8767819a72SFangrui Song std::optional<APInt> FoldGAddMix = ConstantFoldBinOp(
8867819a72SFangrui Song TargetOpcode::G_ADD, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
893361a364SKazu Hirata EXPECT_TRUE(FoldGAddMix.has_value());
9067ba5c50SFangrui Song EXPECT_EQ(1073741840ULL, FoldGAddMix->getLimitedValue());
910112123eSMarcello Maggioni
920112123eSMarcello Maggioni // Test G_AND folding Integer + Mixed Int-Float cases
9367819a72SFangrui Song std::optional<APInt> FoldGAndInt = ConstantFoldBinOp(
9467819a72SFangrui Song TargetOpcode::G_AND, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
953361a364SKazu Hirata EXPECT_TRUE(FoldGAndInt.has_value());
9667ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGAndInt->getLimitedValue());
9767819a72SFangrui Song std::optional<APInt> FoldGAndMix = ConstantFoldBinOp(
9867819a72SFangrui Song TargetOpcode::G_AND, MIBCst2.getReg(0), MIBFCst1.getReg(0), *MRI);
993361a364SKazu Hirata EXPECT_TRUE(FoldGAndMix.has_value());
10067ba5c50SFangrui Song EXPECT_EQ(1ULL, FoldGAndMix->getLimitedValue());
1010112123eSMarcello Maggioni
1020112123eSMarcello Maggioni // Test G_ASHR folding Integer + Mixed cases
10367819a72SFangrui Song std::optional<APInt> FoldGAShrInt = ConstantFoldBinOp(
10467819a72SFangrui Song TargetOpcode::G_ASHR, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1053361a364SKazu Hirata EXPECT_TRUE(FoldGAShrInt.has_value());
10667ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGAShrInt->getLimitedValue());
10767819a72SFangrui Song std::optional<APInt> FoldGAShrMix = ConstantFoldBinOp(
10867819a72SFangrui Song TargetOpcode::G_ASHR, MIBFCst2.getReg(0), MIBCst2.getReg(0), *MRI);
1093361a364SKazu Hirata EXPECT_TRUE(FoldGAShrMix.has_value());
11067ba5c50SFangrui Song EXPECT_EQ(2097152ULL, FoldGAShrMix->getLimitedValue());
1110112123eSMarcello Maggioni
1120112123eSMarcello Maggioni // Test G_LSHR folding Integer + Mixed Int-Float cases
11367819a72SFangrui Song std::optional<APInt> FoldGLShrInt = ConstantFoldBinOp(
11467819a72SFangrui Song TargetOpcode::G_LSHR, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1153361a364SKazu Hirata EXPECT_TRUE(FoldGLShrInt.has_value());
11667ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGLShrInt->getLimitedValue());
11767819a72SFangrui Song std::optional<APInt> FoldGLShrMix = ConstantFoldBinOp(
11867819a72SFangrui Song TargetOpcode::G_LSHR, MIBFCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1193361a364SKazu Hirata EXPECT_TRUE(FoldGLShrMix.has_value());
12067ba5c50SFangrui Song EXPECT_EQ(2080768ULL, FoldGLShrMix->getLimitedValue());
1210112123eSMarcello Maggioni
1220112123eSMarcello Maggioni // Test G_MUL folding Integer + Mixed Int-Float cases
12367819a72SFangrui Song std::optional<APInt> FoldGMulInt = ConstantFoldBinOp(
12467819a72SFangrui Song TargetOpcode::G_MUL, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1253361a364SKazu Hirata EXPECT_TRUE(FoldGMulInt.has_value());
12667ba5c50SFangrui Song EXPECT_EQ(144ULL, FoldGMulInt->getLimitedValue());
12767819a72SFangrui Song std::optional<APInt> FoldGMulMix = ConstantFoldBinOp(
12867819a72SFangrui Song TargetOpcode::G_MUL, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1293361a364SKazu Hirata EXPECT_TRUE(FoldGMulMix.has_value());
13067ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGMulMix->getLimitedValue());
1310112123eSMarcello Maggioni
1320112123eSMarcello Maggioni // Test G_OR folding Integer + Mixed Int-Float cases
13367819a72SFangrui Song std::optional<APInt> FoldGOrInt = ConstantFoldBinOp(
13467819a72SFangrui Song TargetOpcode::G_OR, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1353361a364SKazu Hirata EXPECT_TRUE(FoldGOrInt.has_value());
13667ba5c50SFangrui Song EXPECT_EQ(25ULL, FoldGOrInt->getLimitedValue());
13767819a72SFangrui Song std::optional<APInt> FoldGOrMix = ConstantFoldBinOp(
13867819a72SFangrui Song TargetOpcode::G_OR, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1393361a364SKazu Hirata EXPECT_TRUE(FoldGOrMix.has_value());
14067ba5c50SFangrui Song EXPECT_EQ(1073741840ULL, FoldGOrMix->getLimitedValue());
1410112123eSMarcello Maggioni
1420112123eSMarcello Maggioni // Test G_SHL folding Integer + Mixed Int-Float cases
14367819a72SFangrui Song std::optional<APInt> FoldGShlInt = ConstantFoldBinOp(
14467819a72SFangrui Song TargetOpcode::G_SHL, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1453361a364SKazu Hirata EXPECT_TRUE(FoldGShlInt.has_value());
14667ba5c50SFangrui Song EXPECT_EQ(8192ULL, FoldGShlInt->getLimitedValue());
14767819a72SFangrui Song std::optional<APInt> FoldGShlMix = ConstantFoldBinOp(
14867819a72SFangrui Song TargetOpcode::G_SHL, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1493361a364SKazu Hirata EXPECT_TRUE(FoldGShlMix.has_value());
15067ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGShlMix->getLimitedValue());
1510112123eSMarcello Maggioni
1520112123eSMarcello Maggioni // Test G_SUB folding Integer + Mixed Int-Float cases
15367819a72SFangrui Song std::optional<APInt> FoldGSubInt = ConstantFoldBinOp(
15467819a72SFangrui Song TargetOpcode::G_SUB, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1553361a364SKazu Hirata EXPECT_TRUE(FoldGSubInt.has_value());
15667ba5c50SFangrui Song EXPECT_EQ(7ULL, FoldGSubInt->getLimitedValue());
15767819a72SFangrui Song std::optional<APInt> FoldGSubMix = ConstantFoldBinOp(
15867819a72SFangrui Song TargetOpcode::G_SUB, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1593361a364SKazu Hirata EXPECT_TRUE(FoldGSubMix.has_value());
16067ba5c50SFangrui Song EXPECT_EQ(3221225488ULL, FoldGSubMix->getLimitedValue());
1610112123eSMarcello Maggioni
1620112123eSMarcello Maggioni // Test G_XOR folding Integer + Mixed Int-Float cases
16367819a72SFangrui Song std::optional<APInt> FoldGXorInt = ConstantFoldBinOp(
16467819a72SFangrui Song TargetOpcode::G_XOR, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1653361a364SKazu Hirata EXPECT_TRUE(FoldGXorInt.has_value());
16667ba5c50SFangrui Song EXPECT_EQ(25ULL, FoldGXorInt->getLimitedValue());
16767819a72SFangrui Song std::optional<APInt> FoldGXorMix = ConstantFoldBinOp(
16867819a72SFangrui Song TargetOpcode::G_XOR, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1693361a364SKazu Hirata EXPECT_TRUE(FoldGXorMix.has_value());
17067ba5c50SFangrui Song EXPECT_EQ(1073741840ULL, FoldGXorMix->getLimitedValue());
1710112123eSMarcello Maggioni
1720112123eSMarcello Maggioni // Test G_UDIV folding Integer + Mixed Int-Float cases
17367819a72SFangrui Song std::optional<APInt> FoldGUdivInt = ConstantFoldBinOp(
17467819a72SFangrui Song TargetOpcode::G_UDIV, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1753361a364SKazu Hirata EXPECT_TRUE(FoldGUdivInt.has_value());
17667ba5c50SFangrui Song EXPECT_EQ(1ULL, FoldGUdivInt->getLimitedValue());
17767819a72SFangrui Song std::optional<APInt> FoldGUdivMix = ConstantFoldBinOp(
17867819a72SFangrui Song TargetOpcode::G_UDIV, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1793361a364SKazu Hirata EXPECT_TRUE(FoldGUdivMix.has_value());
18067ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGUdivMix->getLimitedValue());
1810112123eSMarcello Maggioni
1820112123eSMarcello Maggioni // Test G_SDIV folding Integer + Mixed Int-Float cases
18367819a72SFangrui Song std::optional<APInt> FoldGSdivInt = ConstantFoldBinOp(
18467819a72SFangrui Song TargetOpcode::G_SDIV, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1853361a364SKazu Hirata EXPECT_TRUE(FoldGSdivInt.has_value());
18667ba5c50SFangrui Song EXPECT_EQ(1ULL, FoldGSdivInt->getLimitedValue());
18767819a72SFangrui Song std::optional<APInt> FoldGSdivMix = ConstantFoldBinOp(
18867819a72SFangrui Song TargetOpcode::G_SDIV, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1893361a364SKazu Hirata EXPECT_TRUE(FoldGSdivMix.has_value());
19067ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGSdivMix->getLimitedValue());
1910112123eSMarcello Maggioni
1920112123eSMarcello Maggioni // Test G_UREM folding Integer + Mixed Int-Float cases
19367819a72SFangrui Song std::optional<APInt> FoldGUremInt = ConstantFoldBinOp(
19467819a72SFangrui Song TargetOpcode::G_UDIV, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
1953361a364SKazu Hirata EXPECT_TRUE(FoldGUremInt.has_value());
19667ba5c50SFangrui Song EXPECT_EQ(1ULL, FoldGUremInt->getLimitedValue());
19767819a72SFangrui Song std::optional<APInt> FoldGUremMix = ConstantFoldBinOp(
19867819a72SFangrui Song TargetOpcode::G_UDIV, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
1993361a364SKazu Hirata EXPECT_TRUE(FoldGUremMix.has_value());
20067ba5c50SFangrui Song EXPECT_EQ(0ULL, FoldGUremMix->getLimitedValue());
2010112123eSMarcello Maggioni
2020112123eSMarcello Maggioni // Test G_SREM folding Integer + Mixed Int-Float cases
20367819a72SFangrui Song std::optional<APInt> FoldGSremInt = ConstantFoldBinOp(
20467819a72SFangrui Song TargetOpcode::G_SREM, MIBCst1.getReg(0), MIBCst2.getReg(0), *MRI);
2053361a364SKazu Hirata EXPECT_TRUE(FoldGSremInt.has_value());
20667ba5c50SFangrui Song EXPECT_EQ(7ULL, FoldGSremInt->getLimitedValue());
20767819a72SFangrui Song std::optional<APInt> FoldGSremMix = ConstantFoldBinOp(
20867819a72SFangrui Song TargetOpcode::G_SREM, MIBCst1.getReg(0), MIBFCst2.getReg(0), *MRI);
2093361a364SKazu Hirata EXPECT_TRUE(FoldGSremMix.has_value());
21067ba5c50SFangrui Song EXPECT_EQ(16ULL, FoldGSremMix->getLimitedValue());
2110112123eSMarcello Maggioni }
2120112123eSMarcello Maggioni
213ff384481SMarcello Maggioni } // namespace
214