https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127358
Bug ID: 127358
Summary: Two-value select fold introduces signed-overflow UB
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kyuwoncho18 at gmail dot com
Target Milestone: ---
Miscompiles valid C/C++ (`-O0` and `-O2/-O3` disagree on a UB-free program
Reproducer:
```
#include <stdio.h>
__attribute__((noinline))
int compute(unsigned x){
if (x != 0x7FFFFFFFu && x != 0x80000000u) __builtin_unreachable();
int r = (x == 0x7FFFFFFFu) ? 5 : 6;
return r;
}
int main(void){
volatile unsigned a = 0x7FFFFFFFu;
volatile unsigned b = 0x80000000u;
printf("%d %d\n", compute(a), compute(b)); /* expect 5 6 */
return 0;
}
```
Result:
O0: 5 6
O1/2/3: 5 5
Check with https://gcc.godbolt.org/z/7dMEE9GE4.
Reproduced between 14.1 ~ trunk.