https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129193
Bug ID: 129193
Summary: [16/17 Regression] wrong code at -O1 and above:
combine drops the NOT in (long) (~x >> 27) < 0 for
__int128 x since r16-2614
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$cat repro.c
__attribute__((noipa)) int f (__int128 x)
{
return (long) (~x >> 27) < 0;
}
int main (void)
{
/* ~0 >> 27 = -1, so (long) -1 < 0. */
if (f (0) != 1)
__builtin_abort ();
return 0;
}
$ gcc -O0 repro.c && ./a.out # ok
$ gcc -O1 repro.c && ./a.out # also -O2, -O3, -Os
Aborted
$ gcc -O1 -fdisable-rtl-combine repro.c && ./a.out # ok
$ gcc -O1 -fno-tree-ter repro.c && ./a.out # ok
Expected: `~0 >> 27` is -1 (arithmetic shift of a negative `__int128`), its low
64 bits are -1, so `f (0)` returns 1.
Actual: `f (0)` returns 0.
## Bisect
**Bisected to r16-2614** (965564eafb7, "simplify-rtx: Simplify subregs of logic
ops", Richard Sandiford, 2025-07-29):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=965564eafb721f8000013a3112f1bba8d8fae32b
The commit distributes lowpart subregs through AND/IOR/XOR with a constant when
one term disappears. Here the NOT (`xor:TI x -1`) sits under the lowpart subreg
of a TImode shift, and the combined pattern above has lost it.
`git bisect run` between basepoints/gcc-16 (7eac294e087, ok) and
basepoints/gcc-17 (c470e700e29, aborts), C-only builds,
repro.c at -O2, no skipped commits:
| commit | repro.c -O2 |
|---|---|
| cc9c041fd1c (testsuite: Generalise aarch64/saturating_arithmetic*.c) | ok |
| 9c3f2cb35d0 (testsuite: Make aarch64/cmpbr.c more forgiving) | ok |
| **965564eafb7 (r16-2614) simplify-rtx: Simplify subregs of logic ops** |
**aborts** |
| 241380c6d63 (aarch64: Add support for unpacked SVE FP conditional binary
arithmetic) | aborts |
## Checked versions
- gcc trunk c10b819941b (2026-11-30, --enable-checking): -O1, -O2, -O3 and -Os
abort; -O0 ok.
- gcc 16/15/14/13: ok at every level.
- clang 21, clang trunk: ok at every level.
- UB: none; clean under ASan+UBSan and MSan (clang), and gcc -O0 and clang
agree.
Godbolt (gcc trunk, -O1 vs -O0): https://godbolt.org/z/jn9vnjE8c
Thanks.