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

            Bug ID: 129208
           Summary: [17 Regression] wrong code at -O2: high-part multiply
                    pattern drops the extra shift in (x * C) >> 18
           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 *, ...);
char a[8];
short b[8];
int main(void) {
  for (int i = 0; i < 8; i++)
    a[i] = 1;
  for (int i = 0; i < 7; i++)
    b[i] = (((unsigned _BitInt(16))a[i] << 15) * (_BitInt(34))65535) >> 18;
  printf("%d\n", (int)b[0]);
  return 0;
}

$ gcc -std=c23 -O1 t.c && ./a.out
8191
$ gcc -std=c23 -O2 t.c && ./a.out
32767

Wrong at -O2, -O3. -fno-tree-vectorize gives 8191.

Bisected to r17-3034-g9cf7c326640 ("vect: Recognise high-part multiply when the
wide type has no vector form").
vect_recog_mulhs_pattern turns ((x << 15) * 65535) >> 18 with a 34-bit product
into .MULH, which is the >> 16 high part, and drops the remaining >> 2: 32767
instead of 8191.

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