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

            Bug ID: 127767
           Summary: Wrong code issue at -O2 on the trunk version
           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 eliminates a live code block (if-then-branch) for some
reason. Please look at the details below.

$cat small.c
#include <stdint.h>
#include <stdlib.h>
#include<stdio.h>

int idx = 0;
int32_t d, g;
int8_t e[9][3][9];
static uint64_t f[1][1][3] = {{{4, 4, 78}}};
int16_t aa;

void __attribute__((noinline)) marker_43(void) { idx++; }

int8_t a(int8_t c, int b) {
  return c < 0 || b < 0 || b >= 32 || c > 127 >> b ? 1 : c << b;
}

static uint8_t k(uint32_t u, int16_t aj, int64_t v) {
  (void)u; (void)v;
  for (; g <= 5; g++)
    if (a(aj, 6))
      ;
    else
      marker_43();
  return 0;
}

uint8_t j(uint64_t u, uint32_t l, int16_t ae, int32_t *m) {
  int16_t af = 2;
  uint32_t ag[6][6] = {{0}};
  int16_t *ah = &aa;
  (void)u; (void)l; (void)m;
  k(af, *ah ^= f[0][0][2], ag[5][0]);
  return ae;
}

int main(void) {
  uint32_t l = 8;
  int32_t *m = &d;
  int i;
  aa = 78;
  for (i = 0; i < 9; i++)
    j(e[0][2][1], l, e[8][1][3], m);
  printf("%d\n", idx);
  return 0;
}


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

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

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

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

$gcc-16 -w -std=c99 -O2 small.c -o t4 ; ./t4
6

Interestingly, the function call `marker_43` is eliminated in the binary t2.

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


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


$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 20261006 (experimental) (GCC)

Reply via email to