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

            Bug ID: 129100
           Summary: [17 Regression] wrong code at -O2: vectorized do-while
                    with an unsigned char index stores to a[256] and
                    beyond
           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/hEPvorsGd

Note:
- works: 15.3, 16.2
- fails: trunk (20261122 snapshot, 871ec62eab32) at -O2 and -O3; aarch64 at -O3
- only when hi == lo (256 iterations) and lo != 0
- -fno-tree-loop-vectorize makes it disappear
- not bisected; r17-5480 added the no-wrap assumption shown below
- PR 128405 gets the same assumption for the load form of this loop

$ gcc-17 -v
Using built-in specs.
COLLECT_GCC=gcc-17
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-17-20261122/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 20261122 (experimental) (GCC)
$ gcc-17 -O2 t.c && ./a.out
Aborted (core dumped)
$ gcc-17 -O2 -fno-tree-loop-vectorize t.c && ./a.out
$ cat t.c
unsigned char b[512];

__attribute__((noipa)) void
f (unsigned char *a, unsigned char lo, unsigned char hi)
{
  unsigned char i = lo;
  do
    a[i++] = 1;
  while (i != hi);
}

int
main (void)
{
  f (b, 200, 200);              /* 256 iterations, i wraps from 255 to 0 */
  for (int j = 256; j < 512; j++)
    if (b[j] != 0)
      __builtin_abort ();
  return 0;
}

f should store to b[0] .. b[255] but stores to b[200] .. b[455].
No warnings with -Wall -Wextra.
Same with -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations.
-fsanitize=address,undefined is clean with gcc 16.2.

$ gcc-17 -O2 -fdump-tree-vect-details -c t.c && grep no-wrap t.c.*.vect
t.c:9:12: note:  adding no-wrap assumption: hi_10(D) - lo_5(D) <= ~lo_5(D)

With hi == lo the left side is 0, not 256, so it holds although i wraps.

Reply via email to