On Thu, Oct 29, 2026 at 1:18 PM Daniel Barboza
<[email protected]> wrote:
>
> Add a couple of match.pd patterns that simplifies a bit_and using a
> bit_xor and a bit_ior with the same operands:
>
> (X ^ Y) & (X | Y) -> X ^ Y
>
> For boolean values there is also a NE variant:
>
> (X NE Y) & (X | Y) -> X NE Y
>
> Also, for boolean values, we're also handling chained bit_iors:
>
> (X ^ Y)  & (Z | X | Y) -> (X ^ Y)
> (X NE Y) & (Z | X | Y) -> (X NE Y)
>
> The takeaway is that if we know that X != Y then X != Y & (X | Y | ...)
> is always going to be the same as X != Y.
>
> The remaining PR115547 patterns that uses "&&" generate chained ifconds
> that can't be handled via match.pd.  We'll try our luck handling those
> inside one of the passes at a later date.
>
>         PR tree-optimization/115547
>
> gcc/ChangeLog:
>
>         * match.pd(`(X ^ Y) & (X | Y)`): New pattern.
>         (`(X NE Y) & (X | Y)`): New pattern.
>         (`(X NE Y) & (Z | X | Y)`): New pattern.
>         (`(X ^ Y) & (Z | X | Y)`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/pr115547.c: New test.
> ---
>  gcc/match.pd                             | 24 ++++++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/pr115547.c | 31 ++++++++++++++++++++++++
>  2 files changed, 55 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 0bda1efac59..fca0981ed49 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -7175,6 +7175,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>                || (op == BIT_AND_EXPR && wi::eq_p (wi::bit_not (c2), c1))))
>           (bit_xor @0 @1)))))
>
> +/* PR115547: (X ^ Y) & (X | Y) -> (X ^ Y).  */
> +(simplify
> + (bit_and:c (bit_xor:c @X @Y) (bit_ior:c @X @Y))
> + (bit_xor @X @Y))

You should just reuse the bit_xor here.
And the inner bit_xor/bit_iors don't need `:c` on them for
canonicalization reasons.

> +
> +/* PR115547: (X NE Y) & (X | Y) -> (X NE Y)
> +   Variant from above when X and Y are booleans/zero_one.  */
> +(simplify
> + (bit_and:c (ne:c zero_one_valued_p@X zero_one_valued_p@Y)
> +           (bit_ior:c @X @Y))
> + (ne @X @Y))

Same points about `:c` as above.  Here we want to have the non reuse
of `ne` so it gets proped into a GIMPLE_COND.

> +
> +/* PR115547:
> +   (X ^ Y) & (Z | X | Y) -> (X ^ Y)
> +   (X NE Y) & (Z | X | Y) -> (X NE Y)
> +
> +   For X, Y and Z being booleans/zero_one values.  */
> +(for op (bit_xor ne)
> + (simplify
> +  (bit_and:c
> +    (op:c zero_one_valued_p@X zero_one_valued_p@Y)
> +    (bit_ior:c @Y (bit_ior:c zero_one_valued_p@Z @X)))
> +  (op @X @Y)))

Same comments about reuse as above; though for ne we don't want the
reuse but for xor we do.  I don't think the `:c` can be removed here
though.

> +
>  (simplify
>   (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
>   (if (INTEGRAL_TYPE_P (type)
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
> new file mode 100644
> index 00000000000..3f8367d67f4
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
> @@ -0,0 +1,31 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-optimized" } */
> +
> +int fn6 (int a, int c) {
> +    return (a ^ c) & (a | c);
> +}
> +
> +_Bool fn6_alt (_Bool a, _Bool c) {
> +    return (a != c) & (a | c);
> +}
> +
> +_Bool fn2_alt (_Bool a, _Bool b, _Bool c) {
> +    return (a ^ c) & (b | c | a);
> +}
> +
> +_Bool fn2_alt2 (_Bool a, _Bool b, _Bool c) {
> +    return (a ^ c) & (b | a | c);
> +}
> +
> +_Bool fn2_alt3 (_Bool a, _Bool b, _Bool c) {
> +    return (a != c) & (b | c | a);
> +}
> +
> +_Bool fn2_alt4 (_Bool a, _Bool b, _Bool c) {
> +    return (a != c) & (b | a | c);
> +}
> +
> +/* { dg-final { scan-tree-dump-times " \\\^ " 3 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times " != " 3 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times " \\\| " 0 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times " & " 0 "optimized" } } */
> --
> 2.43.0
>

Reply via email to