[llvm] [PatternMatching] Add generic API for matching constants using custom conditions (PR #85676)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu May 2 10:15:35 PDT 2024
================
@@ -611,6 +611,212 @@ TEST_F(PatternMatchTest, BitCast) {
EXPECT_FALSE(m_ElementWiseBitCast(m_Value()).match(NXV2I64ToNXV4I32));
}
+TEST_F(PatternMatchTest, CustomCheckFn) {
+ APInt I0(64, 0);
+ APInt I1(64, 0);
+
+ auto CheckIsZeroI = [](const APInt &C) { return C.isZero(); };
+ auto CheckIsEqI1 = [&I1](const APInt &C) { return C.eq(I1); };
+ auto CheckIsNeI1 = [&I1](const APInt &C) { return !C.eq(I1); };
+
+ custom_checkfn<APInt> CustomCheckZeroI;
+ CustomCheckZeroI.CheckFn = CheckIsZeroI;
+ custom_checkfn<APInt> CustomCheckEqI1;
+ CustomCheckEqI1.CheckFn = CheckIsEqI1;
+ custom_checkfn<APInt> CustomCheckNeI1;
+ CustomCheckNeI1.CheckFn = CheckIsNeI1;
+
+ EXPECT_TRUE(CustomCheckZeroI.isValue(I0));
+ EXPECT_TRUE(CustomCheckEqI1.isValue(I0));
+ EXPECT_FALSE(CustomCheckNeI1.isValue(I0));
+
+ I0.setBit(0);
+
+ EXPECT_FALSE(CustomCheckZeroI.isValue(I0));
+ EXPECT_FALSE(CustomCheckEqI1.isValue(I0));
+ EXPECT_TRUE(CustomCheckNeI1.isValue(I0));
+
+ I1.setBit(0);
+
+ EXPECT_FALSE(CustomCheckZeroI.isValue(I0));
+ EXPECT_TRUE(CustomCheckEqI1.isValue(I0));
+ EXPECT_FALSE(CustomCheckNeI1.isValue(I0));
+
+ APFloat F0(0.0);
+ APFloat F1(0.0);
+
+ auto CheckIsZeroF = [](const APFloat &C) { return C.isZero(); };
+ auto CheckIsEqF1 = [&F1](const APFloat &C) {
+ return C.bitcastToAPInt().eq(F1.bitcastToAPInt());
+ };
+ auto CheckIsNeF1 = [&F1](const APFloat &C) {
+ return !C.bitcastToAPInt().eq(F1.bitcastToAPInt());
+ };
+
+ custom_checkfn<APFloat> CustomCheckZeroF;
+ CustomCheckZeroF.CheckFn = CheckIsZeroF;
+ custom_checkfn<APFloat> CustomCheckEqF1;
+ CustomCheckEqF1.CheckFn = CheckIsEqF1;
+ custom_checkfn<APFloat> CustomCheckNeF1;
+ CustomCheckNeF1.CheckFn = CheckIsNeF1;
+
+ EXPECT_TRUE(CustomCheckZeroF.isValue(F0));
+ EXPECT_TRUE(CustomCheckEqF1.isValue(F0));
+ EXPECT_FALSE(CustomCheckNeF1.isValue(F0));
+
+ F0 = -F0;
+
+ EXPECT_TRUE(CustomCheckZeroF.isValue(F0));
+ EXPECT_FALSE(CustomCheckEqF1.isValue(F0));
+ EXPECT_TRUE(CustomCheckNeF1.isValue(F0));
+
+ F0 = -F0;
+
+ EXPECT_TRUE(CustomCheckZeroF.isValue(F0));
+ EXPECT_TRUE(CustomCheckEqF1.isValue(F0));
+ EXPECT_FALSE(CustomCheckNeF1.isValue(F0));
+
+ F0 = F0 + APFloat(1.0);
+
+ EXPECT_FALSE(CustomCheckZeroF.isValue(F0));
+ EXPECT_FALSE(CustomCheckEqF1.isValue(F0));
+ EXPECT_TRUE(CustomCheckNeF1.isValue(F0));
+
+ F1 = F1 + APFloat(1.0);
+
+ EXPECT_FALSE(CustomCheckZeroF.isValue(F0));
+ EXPECT_TRUE(CustomCheckEqF1.isValue(F0));
+ EXPECT_FALSE(CustomCheckNeF1.isValue(F0));
+}
----------------
dtcxzyw wrote:
> do we really need unit tests for it if you already have tests for m_CheckedInt below?
The version I reviewed https://github.com/llvm/llvm-project/commit/4d7774dfdae01dbc8ebb9b30748cfb8638240ca3 doesn't include tests for m_CheckedInt.
It is ok to remove tests for CustomCheckFn.
https://github.com/llvm/llvm-project/pull/85676
More information about the llvm-commits
mailing list