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

            Bug ID: 129190
           Summary: [14/15/16/17 Regression] wrong code at -O1/-O2/-Os on
                    powerpc64le: doloop zero-trip test wraps for a
                    power-of-2 step
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hhhuang at smu dot edu.sg
  Target Milestone: ---
            Target: powerpc64le-*-*, powerpc-*-*, s390x-*-*

Compiler Explorer (assembly only): https://godbolt.org/z/Tjxo5xc81

Note:
- fails at -O1, -O2, -Os: powerpc64le 16.2, trunk bc49f1e37014 (20261127)
- 11.2 to 16.1 fail too; 6.3 and IBM Advance Toolchain 13.0 (gcc 9.2.1)
  work; 10 not tried (assembly from Compiler Explorer, linked and run
  under QEMU here)
- s390x and 32-bit powerpc fail at -Os
- -fno-branch-count-reg hides it
- -fwrapv or -fno-aggressive-loop-optimizations hides it: the loop is
  then not known to be finite. With an unsigned n it fails with
  -ffinite-loops, and as C++ at plain -O2
- not bisected; see the end for a test with the r10-1367 change disabled

$ powerpc64le-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=powerpc64le-linux-gnu-gcc-17
Target: powerpc64le-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
--target=powerpc64le-linux-gnu --program-prefix=powerpc64le-linux-gnu-
--with-sysroot=/usr/powerpc64le-linux-gnu
--with-native-system-header-dir=/include
--with-as=/usr/bin/powerpc64le-linux-gnu-as
--with-ld=/usr/bin/powerpc64le-linux-gnu-ld --disable-shared --disable-threads
--disable-libssp --disable-libgomp --disable-libquadmath --disable-libatomic
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261127 (experimental) (GCC)
$ powerpc64le-linux-gnu-gcc-17 -O2 -Wall -Wextra -static t.c
$ qemu-ppc64le ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ powerpc64le-linux-gnu-gcc-17 -O2 -fno-branch-count-reg -static t.c
$ qemu-ppc64le ./a.out
$ cat t.c
unsigned a[8];

__attribute__((noipa)) int
f (unsigned e, unsigned i)
{
  int n = 0;
  while (i > e)
    {
      a[n++ & 7] = 1;
      i -= 4;
    }
  return n;
}

int
main (void)
{
  if (f (0, 8) != 2)
    __builtin_abort ();
  return 0;
}

f (0, 8) must return 2: the body runs for i = 8 and i = 4, then 0 > 0 is false.
Nothing wraps or overflows.
It returns 1 at -O1 and -O2 and 0 at -Os.
No warnings with -Wall -Wextra; ASan/UBSan report nothing (x86_64 16.2, -O0).

$ powerpc64le-linux-gnu-gcc-17 -O2 -fdump-rtl-loop2_unroll-details -c t.c
$ sed -n '/does not roll if/,/updated to finite/p' t.c.*.loop2_unroll
  does not roll if: (expr_list:REG_DEP_TRUE (gtu:SI (plus:SI (subreg:SI
(reg/v:DI 129 [ e ]) 0)
            (const_int -3 [0xfffffffffffffffd]))
        (plus:SI (subreg:SI (reg/v:DI 130 [ i ]) 0)
            (const_int -4 [0xfffffffffffffffc])))
    (nil))
  infinite if: (expr_list:REG_DEP_TRUE (and:SI (gtu:SI (const_int 4 [0x4])
            (plus:SI (subreg:SI (reg/v:DI 129 [ e ]) 0)
                (const_int 1 [0x1])))
        (leu:SI (and:SI (plus:SI (subreg:SI (reg/v:DI 129 [ e ]) 0)
                    (const_int 1 [0x1]))
                (const_int 3 [0x3]))
            (and:SI (subreg:SI (reg/v:DI 130 [ i ]) 0)
                (const_int 3 [0x3]))))
    (nil))
  number of iterations: (lshiftrt:SI (plus:SI (not:SI (subreg:SI (reg/v:DI 129
[ e ]) 0))
        (subreg:SI (reg/v:DI 130 [ i ]) 0))
    (const_int 2 [0x2]))
  upper bound: 2147483646
  likely upper bound: 2147483646
  realistic bound: -1
Assume loop 1 to be finite: it has an exit and -ffinite-loops is on or loop was
previously finite.
  infinite updated to finite.

The number of iterations, (i - e - 1) >> 2, is right. "does not roll if" is
wrong: it is a - s > b (a = e + 1, s = 4, b = i - 4), and a - s wraps for e <
3. For a power-of-2 step iv_number_of_iterations (loop-iv.cc) puts that case
only in desc->infinite, and only for a % s <= b % s; here a % s > b % s.
doloop gives up when desc->infinite is set, but since r10-1367 (PR 62147)
find_simple_exit clears it for a loop known to be finite. With that reset
disabled in trunk, t.c passes at -O1, -O2 and -Os.

Reply via email to