https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129222
Bug ID: 129222
Summary: [16/17 Regression] UBSan runtime error: signed integer
overflow `val + 1` with val == LONG_MAX in
noce_splat_to_const (ifcvt.cc) for `b < 0 ? 0 :
LONG_MAX` since r16-3350
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: haoxintu at gmail dot com
Target Milestone: ---
Hi,
$cat repro.c
long f (long b) { return b < 0 ? 0 : 0x7fffffffffffffffL; }
$ gcc -O2 -c repro.c # also -O1, -Os
gcc/ifcvt.cc:1412:23: runtime error: signed integer overflow:
9223372036854775807 + 1 cannot be represented in type 'long int'
#0 in noce_splat_to_const gcc/ifcvt.cc:1412
#1 in noce_try_sign_bit_splat gcc/ifcvt.cc:1550
#2 in noce_process_if_block gcc/ifcvt.cc:5087
#3 in noce_find_if_block gcc/ifcvt.cc:5530
#4 in find_if_header gcc/ifcvt.cc:5722
#5 in if_convert gcc/ifcvt.cc:6840
#6 in rest_of_handle_if_conversion gcc/ifcvt.cc:6901
## Cause
The expressions came with `noce_try_sign_bit_splat`, **r16-3350** (ebbeaf490c5,
"[PR rtl-optimization/120553] Improve selecting between constants based on sign
bit test", Jeff Law, 2025-08-22):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ebbeaf490c56e04d2e9be25caf9522ef5fba6c72
which added `exact_log2 (val_a + 1)` / `exact_log2 (~val_a + 1)` (and the same
for `val_b`). r17-4062 (5d675c9bef7, "ifcvt: factor the sign-bit-splat constant
selection", 2026-09-06) moved them unchanged into `noce_splat_to_const`, where
they are in current trunk.
## Checked versions
- gcc trunk built with -fsanitize=undefined (113f18c2309, 2026-10-17): the
overflow above at -O1, -O2, -Os.
- gcc trunk c10b819941b and gcc 16.2 / 16.0.1: emit the sign-bit-splat sequence
`sar $63; not; shr $1` for `f`, i.e. go through the overflowing `exact_log2
(val + 1)` test (no sanitizer build of 16 at hand).
Godbolt (gcc trunk and 16.2 emit the splat sequence, 15.2 a cmov; no sanitizer
build available there): https://godbolt.org/z/7v8WhPErq
Thanks.