https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122615
Bug ID: 122615
Summary: `x&a ? (x&~a): (x|a)` -> x ^ a
Product: gcc
Version: 16.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:
```
int f(int x)
{
return x&1 ? (x&~1): (x|1);
}
```
This can be optimized to just `x ^ 1`, this can work with not just constants
but also all.
```
int f0(int x, int a)
{
return x&a ? (x&~a): (x|a);
}
```
Should also be optimized to `x^a`. Note the non-constant version might be
harder to do on the tree level because of the (current) limitation in phiopt
but it can be caught at the generic level though.
Note this is not exactly https://github.com/llvm/llvm-project/issues/167173
(will file a seperate bug for that one) but related (the different is `x-1` vs
`x&~1`).