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

            Bug ID: 129159
           Summary: [14/15/16/17 Regression] wrong code at -Os: final
                    value replacement of (unsigned long) i ignores wrap of
                    unsigned i
           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: hhhuang at smu dot edu.sg
  Target Milestone: ---

Compiler Explorer: https://godbolt.org/z/4nGbbP3z7

Note:
- fails at -Os: 14.1 to 16.2 and trunk bc49f1e37014 (20261127)
- works: 13.4 at -Os; trunk at -O1, -O2, -O3
- trunk -O2 fails with -fno-tree-ch
- -fno-code-hoisting or -fno-aggressive-loop-optimizations hides it
- not bisected
- not PR 128942: -fwrapv fixes that one, not this one

$ gcc-17 -v
Using built-in specs.
COLLECT_GCC=gcc-17
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-17-20261127/configure --prefix=/w/out/gcc-trunk
--program-suffix=-17 --enable-languages=c --disable-bootstrap
--disable-multilib --disable-nls --disable-libsanitizer --enable-multiarch
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261127 (experimental) (GCC)
$ gcc-17 -Os -Wall -Wextra t.c && ./a.out
Aborted (core dumped)
$ gcc-17 -O2 t.c && ./a.out
$ gcc-17 -Os -fno-code-hoisting t.c && ./a.out
$ gcc-17 -Os -fno-aggressive-loop-optimizations t.c && ./a.out
$ cat t.c
unsigned short a[41];

__attribute__ ((noipa)) unsigned long
f (unsigned n)
{
  unsigned i = 38;
  while (i < n)
    {
      a[(unsigned long) i + 2] = a[(unsigned long) i + 1] + a[i];
      i--;
    }
  return i;
}

int
main (void)
{
  if (f (103) != 0xffffffff)
    __builtin_abort ();
  return 0;
}

f (103) must return 0xffffffff; at -Os it returns 0xffffffffffffffff.
The body runs for i = 38 down to 0, so every subscript is inside a[0..40]; then
i wraps to 0xffffffff and the loop ends. i is unsigned: no undefined behaviour.
No warnings with -Wall -Wextra; ASan/UBSan report nothing (16.2, -O0).

$ gcc-17 -Os -fdump-tree-pre -fdump-tree-sccp -c t.c
$ grep -A3 '# i_' t.c.*.pre
  # i_7 = PHI <38(2), i_13(3)>
  _14 = (long unsigned int) i_7;
  if (i_7 < n_10(D))
    goto <bb 3>; [97.50%]
$ grep -A2 'final value replacement' t.c.*.sccp
final value replacement:
  _1 = PHI <_14(4)>
 with expr: n_10(D) <= 38 ? 38 : 18446744073709551615

Code hoisting puts (unsigned long) i in the loop header, so it is also computed
for the last test, where i is 0xffffffff. SCCP replaces its value after the
loop by 38 - 39 in 64 bits: i does not wrap in the 39 runs of the body, but the
header runs 40 times. 13.4 has the same header and does not replace it.
-fno-tree-scev-cprop alone does not help; it takes -fno-ivopts as well.
ivopts then has _14 as a 64-bit IV with base 38 and step 18446744073709551615,
so the wrong evolution seems to come from SCEV.

Reply via email to