> -----Original Message-----
> From: pan2...@intel.com <pan2...@intel.com>
> Sent: Tuesday, May 21, 2024 2:13 AM
> To: gcc-patches@gcc.gnu.org
> Cc: juzhe.zh...@rivai.ai; kito.ch...@gmail.com; Tamar Christina
> <tamar.christ...@arm.com>; richard.guent...@gmail.com; Pan Li
> <pan2...@intel.com>
> Subject: [PATCH v3] Match: Extract ternary_integer_types_match_p helper func
> [NFC]
>
> From: Pan Li <pan2...@intel.com>
>
> There are sorts of match pattern for SAT related cases, there will be
> some duplicated code to check the dest, op_0, op_1 are same tree types.
> Aka ternary tree type matches. Thus, extract one helper function to
> do this and avoid match code duplication.
>
> The below test suites are passed for this patch:
> * The rv64gcv fully regression test.
> * The x86 bootstrap test.
> * The x86 regression test.
>
> gcc/ChangeLog:
>
> * match.pd: Leverage helper func for SAT_ADD match.
> * tree.cc (ternary_integer_types_match_p): New func impl to
> check if ternary tree types are all integer.
> * tree.h (ternary_integer_types_match_p): New func decl.
>
Thanks, looks good to me! You still need approval from a maintainer..
Cheers,
Tamar
> Signed-off-by: Pan Li <pan2...@intel.com>
> ---
> gcc/match.pd | 28 +++++++---------------------
> gcc/tree.cc | 16 ++++++++++++++++
> gcc/tree.h | 5 +++++
> 3 files changed, 28 insertions(+), 21 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 0f9c34fa897..cff67c84498 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -39,7 +39,8 @@ along with GCC; see the file COPYING3. If not see
> HONOR_NANS
> uniform_vector_p
> expand_vec_cmp_expr_p
> - bitmask_inv_cst_vector_p)
> + bitmask_inv_cst_vector_p
> + ternary_integer_types_match_p)
>
> /* Operator lists. */
> (define_operator_list tcc_comparison
> @@ -3046,38 +3047,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> /* Unsigned Saturation Add */
> (match (usadd_left_part_1 @0 @1)
> (plus:c @0 @1)
> - (if (INTEGRAL_TYPE_P (type)
> - && TYPE_UNSIGNED (TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@1)))))
> + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED
> (type))))
>
> (match (usadd_left_part_2 @0 @1)
> (realpart (IFN_ADD_OVERFLOW:c @0 @1))
> - (if (INTEGRAL_TYPE_P (type)
> - && TYPE_UNSIGNED (TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@1)))))
> + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED
> (type))))
>
> (match (usadd_right_part_1 @0 @1)
> (negate (convert (lt (plus:c @0 @1) @0)))
> - (if (INTEGRAL_TYPE_P (type)
> - && TYPE_UNSIGNED (TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@1)))))
> + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED
> (type))))
>
> (match (usadd_right_part_1 @0 @1)
> (negate (convert (gt @0 (plus:c @0 @1))))
> - (if (INTEGRAL_TYPE_P (type)
> - && TYPE_UNSIGNED (TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@1)))))
> + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED
> (type))))
>
> (match (usadd_right_part_2 @0 @1)
> (negate (convert (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1))
> integer_zerop)))
> - (if (INTEGRAL_TYPE_P (type)
> - && TYPE_UNSIGNED (TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@0))
> - && types_match (type, TREE_TYPE (@1)))))
> + (if (ternary_integer_types_match_p (type, @0, @1) && TYPE_UNSIGNED
> (type))))
>
> /* We cannot merge or overload usadd_left_part_1 and usadd_left_part_2
> because the sub part of left_part_2 cannot work with right_part_1.
> diff --git a/gcc/tree.cc b/gcc/tree.cc
> index 6564b002dc1..b59d42c3e47 100644
> --- a/gcc/tree.cc
> +++ b/gcc/tree.cc
> @@ -10622,6 +10622,22 @@ uniform_integer_cst_p (tree t)
> return NULL_TREE;
> }
>
> +/* Check if the types T1, T2 and T3 are effectively the same integer type.
> + If T1, T2 or T3 is not a type, the test applies to their TREE_TYPE. */
> +
> +bool
> +ternary_integer_types_match_p (tree t1, tree t2, tree t3)
> +{
> + t1 = TYPE_P (t1) ? t1 : TREE_TYPE (t1);
> + t2 = TYPE_P (t2) ? t2 : TREE_TYPE (t2);
> + t3 = TYPE_P (t3) ? t3 : TREE_TYPE (t3);
> +
> + if (!INTEGRAL_TYPE_P (t1) || !INTEGRAL_TYPE_P (t2) || !INTEGRAL_TYPE_P
> (t3))
> + return false;
> +
> + return types_compatible_p (t1, t2) && types_compatible_p (t2, t3);
> +}
> +
> /* Checks to see if T is a constant or a constant vector and if each element
> E
> adheres to ~E + 1 == pow2 then return ~E otherwise NULL_TREE. */
>
> diff --git a/gcc/tree.h b/gcc/tree.h
> index ee2aae332a4..4ac59ac55cb 100644
> --- a/gcc/tree.h
> +++ b/gcc/tree.h
> @@ -5212,6 +5212,11 @@ extern bool integer_pow2p (const_tree);
>
> extern tree bitmask_inv_cst_vector_p (tree);
>
> +/* Check if the types T1, T2 and T3 are effectively the same integer type.
> + If T1, T2 or T3 is not a type, the test applies to their TREE_TYPE. */
> +
> +extern bool ternary_integer_types_match_p (tree, tree, tree);
> +
> /* integer_nonzerop (tree x) is nonzero if X is an integer constant
> with a nonzero value. */
>
> --
> 2.34.1