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

            Bug ID: 129205
           Summary: wrong code at -O3: loop splitting truncates a long
                    exit bound to the int IV type
           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: ---
            Target: x86_64-pc-linux-gnu

int printf(const char *, ...);
int a, c = 16;
long b = 9223372036854775776;
int main(void)
{
  do
    a += c < 0 ? 1 : c;
  while ((c -= 8) > b);
  printf("%d\n", c);
  return 0;
}

$ gcc -O2 t.c && ./a.out
8
$ gcc -O3 t.c && ./a.out
-8

Wrong at -O3. -fno-split-loops gives 8.

Bisected to r14-2944-g3d48c11ad08 ("Swap loop splitting and final value
replacement"); its parent is fine. 7.5 and 8.5 fail too, 10.5 to 13.4 do not.
The loop is split at c < 0. compute_new_first_bound computes the bound of the
first loop from b - (long) c converted to the type of c (int). b is near
LONG_MAX, so the converted value is negative and the first loop no longer stops
at the original exit test (c -= 8) > b. With _BitInt IVs the same happens on
trunk since r17-89.

Clean with -fsanitize=address,undefined (gcc and clang).

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

Reply via email to