[llvm] [PatternMatching] Add generic API for matching constants using custom conditions (PR #85676)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 25 22:14:52 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));
+}
----------------
nikic wrote:
Hm, I'd consider custom_checkfn to be a PatternMatch implementation detail -- do we really need unit tests for it if you already have tests for m_CheckedInt below?
https://github.com/llvm/llvm-project/pull/85676
More information about the llvm-commits
mailing list