Skip to content

Commit f23969c

Browse files
committed
test: add MatchZeroOneAllOnes testcase
1 parent 229ec28 commit f23969c

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,92 @@ TEST_F(SelectionDAGPatternMatchTest, matchReassociatableOp) {
825825
EXPECT_FALSE(sd_match(
826826
ORS0123, m_ReassociatableOr(m_Value(), m_Value(), m_Value(), m_Value())));
827827
}
828+
829+
TEST_F(SelectionDAGPatternMatchTest, MatchZeroOneAllOnes) {
830+
using namespace SDPatternMatch;
831+
832+
SDLoc DL;
833+
EVT VT = EVT::getIntegerVT(Context, 32);
834+
835+
// Scalar constant 0
836+
SDValue Zero = DAG->getConstant(0, DL, VT);
837+
EXPECT_TRUE(sd_match(Zero, DAG.get(), llvm::SDPatternMatch::m_Zero()));
838+
EXPECT_FALSE(sd_match(Zero, DAG.get(), m_One()));
839+
EXPECT_FALSE(sd_match(Zero, DAG.get(), m_AllOnes()));
840+
841+
// Scalar constant 1
842+
SDValue One = DAG->getConstant(1, DL, VT);
843+
EXPECT_FALSE(sd_match(One, DAG.get(), m_Zero()));
844+
EXPECT_TRUE(sd_match(One, DAG.get(), m_One()));
845+
EXPECT_FALSE(sd_match(One, DAG.get(), m_AllOnes()));
846+
847+
// Scalar constant -1
848+
SDValue AllOnes =
849+
DAG->getConstant(APInt::getAllOnes(VT.getSizeInBits()), DL, VT);
850+
EXPECT_FALSE(sd_match(AllOnes, DAG.get(), m_Zero()));
851+
EXPECT_FALSE(sd_match(AllOnes, DAG.get(), m_One()));
852+
EXPECT_TRUE(sd_match(AllOnes, DAG.get(), m_AllOnes()));
853+
854+
EVT VecF32 = EVT::getVectorVT(Context, MVT::f32, 4);
855+
EVT VecVT = EVT::getVectorVT(Context, MVT::i32, 4);
856+
857+
// m_Zero: splat vector of 0 → bitcast
858+
{
859+
SDValue SplatVal = DAG->getConstant(0, DL, MVT::i32);
860+
SDValue VecSplat = DAG->getSplatBuildVector(VecVT, DL, SplatVal);
861+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, VecSplat);
862+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_Zero()));
863+
}
864+
865+
// m_One: splat vector of 1 → bitcast
866+
{
867+
SDValue SplatVal = DAG->getConstant(1, DL, MVT::i32);
868+
SDValue VecSplat = DAG->getSplatBuildVector(VecVT, DL, SplatVal);
869+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, VecSplat);
870+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_One()));
871+
}
872+
873+
// m_AllOnes: splat vector of -1 → bitcast
874+
{
875+
SDValue SplatVal = DAG->getConstant(APInt::getAllOnes(32), DL, MVT::i32);
876+
SDValue VecSplat = DAG->getSplatBuildVector(VecVT, DL, SplatVal);
877+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, VecSplat);
878+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_AllOnes()));
879+
}
880+
881+
// splat vector with one undef → default should NOT match
882+
SDValue Undef = DAG->getUNDEF(MVT::i32);
883+
884+
{
885+
// m_Zero: Undef + constant 0
886+
SDValue Zero = DAG->getConstant(0, DL, MVT::i32);
887+
SmallVector<SDValue, 4> Ops(4, Zero);
888+
Ops[2] = Undef;
889+
SDValue Vec = DAG->getBuildVector(VecVT, DL, Ops);
890+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, Vec);
891+
EXPECT_FALSE(sd_match(Bitcasted, DAG.get(), m_Zero()));
892+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_Zero(true)));
893+
}
894+
895+
{
896+
// m_One: Undef + constant 1
897+
SDValue One = DAG->getConstant(1, DL, MVT::i32);
898+
SmallVector<SDValue, 4> Ops(4, One);
899+
Ops[1] = Undef;
900+
SDValue Vec = DAG->getBuildVector(VecVT, DL, Ops);
901+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, Vec);
902+
EXPECT_FALSE(sd_match(Bitcasted, DAG.get(), m_One()));
903+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_One(true)));
904+
}
905+
906+
{
907+
// m_AllOnes: Undef + constant -1
908+
SDValue AllOnes = DAG->getConstant(APInt::getAllOnes(32), DL, MVT::i32);
909+
SmallVector<SDValue, 4> Ops(4, AllOnes);
910+
Ops[0] = Undef;
911+
SDValue Vec = DAG->getBuildVector(VecVT, DL, Ops);
912+
SDValue Bitcasted = DAG->getNode(ISD::BITCAST, DL, VecF32, Vec);
913+
EXPECT_FALSE(sd_match(Bitcasted, DAG.get(), m_AllOnes()));
914+
EXPECT_TRUE(sd_match(Bitcasted, DAG.get(), m_AllOnes(true)));
915+
}
916+
}

0 commit comments

Comments
 (0)