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

            Bug ID: 129206
           Summary: wrong code at -O2 with vectorized ((short) b +
                    (unsigned long long) b + 1) >> 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: ---
            Target: x86_64-pc-linux-gnu

int printf(const char *, ...);
short a[8];
unsigned short b[8];
int main(void) {
  for (int i = 0; i < 8; i++) b[i] = 65535;
  for (int i = 0; i < 5; i++) a[i] = (short)b[i] + (unsigned long long)b[i] + 1
>> 1;
  printf("%d\n", a[0]);
  return 0;
}

$ gcc -O1 t.c && ./a.out
32767
$ gcc -O2 t.c && ./a.out
-1

Wrong at -O2, -O3. -fno-tree-loop-vectorize gives 32767.

Bisected to r10-2593-g94538b6545b ("mmx.md (mmxdoublemode): New mode
attribute."); its parent is fine. That commit only makes 64-bit vectors
available on x86-64.
vect_recog_average_pattern makes .AVG_CEIL on unsigned short. One addend is
(short) b, sign-extended, the other is b, zero-extended, so bit 16 of the sum
is lost: b = 65535 gives -1 instead of 32767.

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