On Thu, May 28, 2026 at 12:06 PM Souradipto Das
<[email protected]> wrote:
>
> This patch introduces a match.pd simplification rule to address a missed
> optimization where expressions of the form (x == 0) | ((x | y) == 0) were
> not reduced down to (x == 0). Utilizing a 2-way macro iterator loop, the
> pattern simultaneously handles the bitwise AND reduction variant:
> (x != 0) & ((x | y) != 0) -> (x != 0).
>
> gcc/ChangeLog:
>
>         PR tree-optimization/125442
>         * match.pd: Add bitop reduction simplifications against zero.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/125442
>         * gcc.dg/int-bwise-opt-1.c: Update existing testcases to _Bool
>         and add new logic reduction patterns.
>         * gcc.dg/int-bwise-opt-2.c: Update return types to _Bool.
>
> Suggested-by: Andrew Pinski <[email protected]>
> Co-authored-by: Kael Franco <[email protected]>
> Signed-off-by: Souradipto Das <[email protected]>
>
> Successfully bootstrapped and regression tested on x86_64-pc-linux-gnu.
> Verified targeted test suite compliance via dejagnu:
>   - gcc.dg/int-bwise-opt-1.c: 6 passes
>   - gcc.dg/int-bwise-opt-2.c: 2 passes
> Totaling 8 expected passes with no regressions.
> ---
>  gcc/match.pd                           |  9 +++++++++
>  gcc/testsuite/gcc.dg/int-bwise-opt-1.c | 18 ++++++++++++++----
>  gcc/testsuite/gcc.dg/int-bwise-opt-2.c |  4 ++--
>  3 files changed, 25 insertions(+), 6 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8a2de136e..2bb4066d3 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -6660,6 +6660,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (simplify
>    (bitop (neeql @0 @1) (neeqr (bit_ior @0 @1) integer_zerop))
>    { constant_boolean_node (bitop == BIT_IOR_EXPR, type); }))
> +
> +/* (a == 0) | ((a | b) == 0) -> (a == 0) -- PR125422
> +   (a != 0) & ((a | b) != 0) -> (a != 0) -- PR125422 */
> +
> +(for bitop (bit_and bit_ior)
> +     neeq  (ne      eq)
> +(simplify
> +  (bitop:c (neeq@2 @0 integer_zerop) (neeq (bit_ior:c @0 @1) integer_zerop))
> +  @2))
>  #endif

This looks good.

>
>  /* These was part of minmax phiopt.  */
> diff --git a/gcc/testsuite/gcc.dg/int-bwise-opt-1.c 
> b/gcc/testsuite/gcc.dg/int-bwise-opt-1.c
> index 11ea9acb3..57a2cc4a6 100644
> --- a/gcc/testsuite/gcc.dg/int-bwise-opt-1.c
> +++ b/gcc/testsuite/gcc.dg/int-bwise-opt-1.c
> @@ -1,26 +1,36 @@
>  /* { dg-do compile } */
>  /* { dg-options "-O2 -fdump-tree-optimized" } */
>
> -int f1(int a, int b)
> +_Bool f1(int a, int b)
>  {
>    return (a != b) & ((a | b) != 0);
>  }
>
> -int f2(int a, int b)
> +_Bool f2(int a, int b)
>  {
>    return (a == b) | ((a | b) == 0);
>  }
>
> -int f3(int a, int b)
> +_Bool f3(int a, int b)
>  {
>    return (a != b) & ((a | b) == 0);
>  }
>
> -int f4(int a, int b)
> +_Bool f4(int a, int b)
>  {
>    return (a == b) | ((a | b) != 0);
>  }
>
> +_Bool f5(int a, int b)
> +{
> +  return (a != 0) & ((a | b) != 0);
> +}
> +
> +_Bool f6(int a, int b)
> +{
> +  return (a == 0) | ((a | b) == 0);
> +}
> +
>  /* { dg-final { scan-tree-dump-times "\\\|" 0 "optimized" } } */
>  /* { dg-final { scan-tree-dump-times "\&" 0 "optimized" } } */

Yes in this case, we should add a new testcase rather than modify the
existing one.
Can you add 2 new testcases called int-bwise-opt-3.c and int-bwise-opt-4.c ?

Thanks,
Andrea

>
> diff --git a/gcc/testsuite/gcc.dg/int-bwise-opt-2.c 
> b/gcc/testsuite/gcc.dg/int-bwise-opt-2.c
> index cc1a48b06..ea7de2c0b 100644
> --- a/gcc/testsuite/gcc.dg/int-bwise-opt-2.c
> +++ b/gcc/testsuite/gcc.dg/int-bwise-opt-2.c
> @@ -1,12 +1,12 @@
>  /* { dg-do compile } */
>  /* { dg-options "-O2 -fdump-tree-optimized" } */
>
> -int f1(int a, int b)
> +_Bool f1(int a, int b)
>  {
>    return (a != b) | ((a | b) != 0);
>  }
>
> -int f2(int a, int b)
> +_Bool f2(int a, int b)
>  {
>    return (a == b) & ((a | b) == 0);
>  }
> --
> 2.43.0
>

Reply via email to