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

            Bug ID: 128636
           Summary: wrong code at -O3: loop splitting on i <= 3 ignores
                    the exit test; do-while runs 4 times instead of 1
           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: jeffchen006 at gmail dot com
  Target Milestone: ---

int printf(const char *, ...);
unsigned f(unsigned z) {
  unsigned a = 0, i = 0;
  do
    if (i <= 3)
      a++;
  while (++i < z);
  return a;
}
unsigned z;
int main(void) { printf("%u\n", f(z)); }

$ gcc -O0 t.c && ./a.out
1
$ gcc -O3 t.c && ./a.out
4

Expected: 1. z = 0, so the do-while body runs once (++i < 0 is false), a = 1.
-O3 (and -O1 -fsplit-loops) gives 4.

Suspected cause: tree-ssa-loop-split: the split on i <= 3 does not keep the
original exit bound.

Avoided by: -fno-split-loops.

Known to fail: 7.5.0, 8.5.0, 10.5.0, 11.4.0, 12.5.0, 13.4.0, 14.3.0, 15.2.0,
16.2.0, 17.0.

-O0 -fsanitize=undefined,address is clean.

gcc version 17.0.0 20261111 (experimental) (a2a11abfad3), x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to