https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124937
Bug ID: 124937
Summary: Wrong code issue at -O1 from GCC-15 to trunk versions
Product: gcc
Version: 16.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,
We found an interesting wrong-code issue with our new testing tool. It seems
gcc-trunk with -O1 eliminates a live code block (if-false-branch) for some
reason. Please look at the details below.
$cat small.c
#include <stdio.h>
#include <stdint.h>
int idx=0;
void __attribute__((noinline)) marker_66() { idx++; }
#define safe_div_func_uint8_t_u_u(_ui1,_ui2) \
({ uint8_t ui1 = (_ui1); uint8_t ui2 = (_ui2) ; \
(((uint8_t)(ui2)) == ((uint8_t)0)) \
? ((uint8_t)(ui1)) \
: (((uint8_t)(ui1)) / ((uint8_t)(ui2)));})
int32_t a;
static int32_t *b(uint32_t e, uint32_t f, uint64_t g);
uint8_t c() {
int32_t *d = &a;
*d ^= 766648612;
b(0, 0, *d);
return 0;
}
int32_t *b(uint32_t e, uint32_t f, uint64_t g) {
int64_t i[][1][1] = {};
if ((safe_div_func_uint8_t_u_u(i[0][0][0], g) || 1) <= g)
;
else
marker_66();
return 0;
}
int main() {
a = 766648612;
c();
printf("%d\n", idx);
}
$gcc-trunk -w -std=c99 -O0 small.c -o t0 ; ./t0
1
$gcc-trunk -w -std=c99 -O1 small.c -o t1 ; ./t1
0
$gcc-trunk -w -std=c99 -O3 small.c -o t2 ; ./t2
1
Interestingly, the function call `marker_66` is eliminated in the binary t1.
$objdump -S t1 | grep marker_66
0000000000401150 <marker_66>:
$objdump -S t1 | grep marker_66
0000000000401128 <marker_66>:
4011c2: e8 61 ff ff ff call 401128 <marker_66>
$gcc-trunk -v
Using built-in specs.
COLLECT_GCC=gcc-trunk
COLLECT_LTO_WRAPPER=/home/haoxin/research/gcc/build/libexec/gcc/x86_64-pc-linux-gnu/16.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/home/haoxin/research/gcc/build
--enable-bootstrap --enable-checking=release --enable-languages=c,c++
--enable-multilib --program-suffix=-trunk : (reconfigured) ../configure
--prefix=/home/haoxin/research/gcc/build --enable-bootstrap
--enable-checking=release --enable-languages=c,c++ --enable-multilib
--program-suffix=-trunk : (reconfigured) ../configure
--prefix=/home/haoxin/research/gcc/build --enable-bootstrap
--enable-checking=release --enable-multilib --program-suffix=-trunk
--enable-languages=c,c++,lto --no-create --no-recursion
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 16.0.1 20260401 (experimental) (GCC)
Reproduced in Godblot: https://godbolt.org/z/ee8r6xb1b. From there, it seems
there is something broken after GCC-15.1.
Could you please help check it?
Thanks,
Haoxin