On 10/30/26 6:05 AM, Daniel Barboza 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.
Bootstrapped and regression tested in x86_64.
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.
---
Changes from v1:
- re-use bit_xor patterns;
- remove redudant inner ":c" qualifiers
- v1 link:https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736840.html
Sashiko called out the lack of :c, but that's clearly an implementation
decision based on feedback from Andrea.
It does seem to think restricting to 0/1 values it not necessary for the
|(X^Y) & (Z|X|Y) → X^Y| case, which seems right to me. Seems like a
trivial follow-up to me.
OK for the trunk.
jeff