[llvm] [PatternMatching] Add generic API for matching constants using custom conditions (PR #85676)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri May 3 17:24:27 PDT 2024


================
@@ -460,6 +460,39 @@ template <typename Predicate> struct apf_pred_ty : public Predicate {
 //
 ///////////////////////////////////////////////////////////////////////////////
 
+template <typename APTy> struct custom_checkfn {
+  function_ref<bool(const APTy &)> CheckFn;
+  bool isValue(const APTy &C) { return CheckFn(C); }
+};
+
+/// Match an integer or vector where CheckFn(ele) for each element is true.
+/// For vectors, poison elements are assumed to match.
+inline cst_pred_ty<custom_checkfn<APInt>>
+m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) {
+  return cst_pred_ty<custom_checkfn<APInt>>{CheckFn};
+}
+
+inline api_pred_ty<custom_checkfn<APInt>>
+m_CheckedInt(const APInt *&V, function_ref<bool(const APInt &)> CheckFn) {
----------------
nikic wrote:

Looking at this again just now, what's the point of these methods returning a `const APInt *`? In that case, isn't this limited to splats anyway and you could just as well use `m_APInt` + a check and save you the lambda?

I think this should be providing the result as a `Constant *`, which would support non-splat values matching the predicate...

https://github.com/llvm/llvm-project/pull/85676


More information about the llvm-commits mailing list