https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113138
Bug ID: 113138
Summary: `x < ~x` can be simplified to `((signed)x) < 0`
Product: gcc
Version: 14.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:
```
#include <stdint.h>
bool srcu(uint32_t x) { return x < ~x; }
bool srcs(int32_t x) { return x < ~x; }
```
But of these can be optimized to:
```
#include <stdint.h>
bool tgt(int32_t x) { return x < 0; }
```
`x >= ~x` can be optimzed to `x >=s 0` (boolean opposites).
While `x > ~x` can be optimized to `(~x) <s 0` or rather `x >s -1` or rather `x
>=s 0`.
While `x <= ~x` can be optimized to `x <s 0`.
So the resulting for loop is:
cmp lt le gt ge
rcmp lt lt ge ge