https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128895
Bug ID: 128895
Summary: `(2 << x) | -(2 << x)` can be optimized to `-2 << x`
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: easyhack, missed-optimization, TREE
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 src(int x)
{
int t = 2 << x;
return (-t) | t;
}
int tgt(int x)
{
int t = 2 << x;
int tneg = -2 << x;
return tneg | t;
}
```
This gets optimized on the rtl level and if we do PR 128894, then this might be
handled on the gimple level too.
We already have a pattern for tgt:
/* Shifts by constants distribute over several binary operations:
(X (<<, >>) C) & (Y (<<, >>) C) -> (X & Y) (<<, >>) C.
(X (<<, >>) C) | (Y (<<, >>) C) -> (X | Y) (<<, >>) C.
(X (<<, >>) C) ^ (Y (<<, >>) C) -> (X ^ Y) (<<, >>) C. */
(for op (bit_and bit_ior bit_xor)
(simplify
(op (shift:s @0 @1) (shift:s @2 @1))
(if (INTEGRAL_TYPE_P (type))
(shift (op @0 @2) @1)))))
Maybe we can add one that handles `(op (neg (shift @0 @1)) (shift @2 @1)))` too
for constants.