https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127447

            Bug ID: 127447
           Summary: Wrong code issue at -O2 and above from GCC-15.1 to
                    17.0 (trunk) versions
           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,

We found an interesting wrong-code issue with our new testing tool, where
gcc-trunk with -O2 and above eliminates a live code block (if-then-branch) for
some reason. Please look at the details below.

$cat small.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int idx = 0;
void __attribute__((noinline)) marker_32(void) { idx++; }

static uint8_t safe_mod_func_uint8_t_u_u(uint8_t ui1, uint8_t ui2) {
  return (ui2 == 0) ? ui1 : (ui1 % ui2);
}

int32_t a;

static void b(int16_t e) {
  if (644 <= safe_mod_func_uint8_t_u_u(~0, e) != e)
    ;
  else
    marker_32();
}

int main(int argc, char *argv[]) {
  a = strtol(argv[1], NULL, 16);
  int32_t k = -8;
  a ^= k;
  b(a);
  printf("%d\n", idx);
  return 0;
}



$gcc-trunk -w -std=c99 -O0 small.c -o t0 ; ./t0 -0x8
1

$gcc-trunk -w -std=c99 -O1 small.c -o t1 ; ./t1 -0x8
1

$gcc-trunk -w -std=c99 -O2 small.c -o t2 ; ./t2 -0x8
0

$gcc-trunk -w -std=c99 -O3 small.c -o t3 ; ./t3 -0x8
0

Interestingly, the function call `marker_32` is eliminated in the binary t2/t3.

$objdump -d t1 | grep marker_32
0000000000401136 <marker_32>:
  401182:       e8 af ff ff ff          call   401136 <marker_32>


$objdump -d t2 | grep marker_32
0000000000401128 <marker_32>:


$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/17.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/home/haoxin/research/gcc/build
--enable-bootstrap--enable-checking=release --enable-multilib
--program-suffix=-trunk --enable-languages=c,c++,lto --disable-werror :
(reconfigured) ../configure --prefix=/home/haoxin/research/gcc/build
--enable-bootstrap --enable-checking=release --enable-multilib
--program-suffix=-trunk --disable-werror --enable-languages=c,c++,lto
--no-create --no-recursion
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 17.0.0 20260911 (experimental) (GCC) 


Reproduced in Godbolt: https://godbolt.org/z/6d1T6dez3. From there, it seems
that something is broken after GCC-15.1.

Could you please help check it?

Thanks,
Haoxin

Reply via email to