On Wed, May 14, 2025 at 7:39 PM Andrew Pinski <[email protected]> wrote:
>
> This is the next step in removing forward_propagate_into_comparison
> and forward_propagate_into_gimple_cond; In the case of `((int)(a cmp b)) != 0`
> we want to do the transformation to `a cmp b` even if the cast is used twice.
> This is exactly what
> forward_propagate_into_comparison/forward_propagate_into_gimple_cond
> do and does the copy.
Actually I am thinking we should change:
this set of patterns:
```
(for cmp (simple_comparison)
(simplify
(cmp (convert@0 @00) (convert?@1 @10))
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
/* Disable this optimization if we're casting a function pointer
type on targets that require function pointer canonicalization. */
&& !(targetm.have_canonicalize_funcptr_for_compare ()
&& ((POINTER_TYPE_P (TREE_TYPE (@00))
&& FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
|| (POINTER_TYPE_P (TREE_TYPE (@10))
&& FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
&& single_use (@0))
```
In the case of:
(if (TREE_CODE (@1) == INTEGER_CST)
(cmp @00 ...)
We don't need to care if @0 is single_use or not as we remove one cast.
Plus all of the cases where we produce constants don't care about
single_use either.
So let's ignore this patch for now. I will get back to it tomorrow.
Thanks,
Andrew
>
> Bootstrapped and tested on x86_64-linux-gnu.
>
> gcc/ChangeLog:
>
> * match.pd (`(a cmp b) != false`, `(a cmp b) == true`,
> `(a cmp b) != true`, `(a cmp b) == false`): Allow an
> optional cast between the comparison and the eq/ne.
> (`bool_val != false`, `bool_val == true`): Allow an optional
> cast between the bool_val and the ne/eq.
>
> Signed-off-by: Andrew Pinski <[email protected]>
> ---
> gcc/match.pd | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 79485f9678a..ffb1695e6e6 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -6913,15 +6913,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (ncmp @0 @1)))))
> /* The following bits are handled by fold_binary_op_with_conditional_arg.
> */
> (simplify
> - (ne (cmp@2 @0 @1) integer_zerop)
> + (ne (convert? (cmp@2 @0 @1)) integer_zerop)
> (if (types_match (type, TREE_TYPE (@2)))
> (cmp @0 @1)))
> (simplify
> - (eq (cmp@2 @0 @1) integer_truep)
> + (eq (convert? (cmp@2 @0 @1)) integer_truep)
> (if (types_match (type, TREE_TYPE (@2)))
> (cmp @0 @1)))
> (simplify
> - (ne (cmp@2 @0 @1) integer_truep)
> + (ne (convert? (cmp@2 @0 @1)) integer_truep)
> (if (types_match (type, TREE_TYPE (@2)))
> (with { enum tree_code ic = invert_tree_comparison
> (cmp, HONOR_NANS (@0)); }
> @@ -6930,7 +6930,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (if (ic == ncmp)
> (ncmp @0 @1))))))
> (simplify
> - (eq (cmp@2 @0 @1) integer_zerop)
> + (eq (convert? (cmp@2 @0 @1)) integer_zerop)
> (if (types_match (type, TREE_TYPE (@2)))
> (with { enum tree_code ic = invert_tree_comparison
> (cmp, HONOR_NANS (@0)); }
> @@ -8104,13 +8104,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
> /* bool_var != 0 becomes bool_var. */
> (simplify
> - (ne @0 integer_zerop)
> + (ne (convert? @0) integer_zerop)
> (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
> && types_match (type, TREE_TYPE (@0)))
> (non_lvalue @0)))
> /* bool_var == 1 becomes bool_var. */
> (simplify
> - (eq @0 integer_onep)
> + (eq (convert? @0) integer_onep)
> (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
> && types_match (type, TREE_TYPE (@0)))
> (non_lvalue @0)))
> --
> 2.43.0
>