https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127222
Bug ID: 127222
Summary: (a & C) + ((~a) & C) -> C
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: enhancement
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Take:
```
unsigned h(unsigned a)
{
return (a&1) + ((~a) &1);
}
```
This will be 1. This works for any lower mask. as the a+~a is always -1. and
(~a)&1 is really ((a&1)^1) reduces to (a&1) + ((a&1) ^1) or rather 0+1 or 1+0.
This works for any value of C (which replaces 1) as the bits of each will be
either set bits of C or 0 for the each side of the addition which forms the
bits of C. Or rather `(a&C) + ((a&C)^C)` since each side is disjoint because of
the flipping of xor; this becomes `(a&C) | ((a&C)^C)` which then just is C.