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

            Bug ID: 128117
           Summary: [17 Regression] wrong code at -O2: code that is not a
                    multiplication is turned into a high-part multiply
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuki.tanaka at defaultcase dot de
  Target Milestone: ---

__attribute__((noipa)) unsigned long long a(unsigned long long x, unsigned long
long y)
{
  unsigned long long xl = x & 0xffffffff, xh = x >> 32;
  unsigned long long yl = y & 0xffffffff, yh = y >> 32;
  unsigned long long t0 = xl * yl;
  unsigned long long t1 = xh * yl;
  unsigned long long t2 = xl * yh;
  unsigned long long t3 = xh * yh;
  unsigned long long s = (t1 & 0xffffffff) + t2 + (t0 >> 32);
  return t3 + (t2 >> 32) + (s >> 32);
}

__attribute__((noipa)) unsigned long long b(unsigned long long x, unsigned long
long y)
{
  unsigned long long xl = x & 0xffffffff, xh = x >> 32;
  unsigned long long yl = y & 0xffffffff, yh = y >> 32;
  unsigned long long t0 = xl * yl;
  unsigned long long t1 = xh * yl;
  unsigned long long t2 = xl * yh;
  unsigned long long t3 = xh * yh;
  unsigned long long u0 = (t0 >> 32) + t1;
  unsigned long long u1 = (u0 & 0xffffffff) + t2;
  unsigned long long u2 = (t0 >> 32) + t2;
  return t3 + (u2 >> 32) + (u1 >> 32);
}

int main ()
{
  if (a(0xffffffff00000000ULL, 0xffffffffULL) != 0)
    __builtin_abort ();
  if (b(0xffffffff00000000ULL, 0xffffffffULL) != 0)
    __builtin_abort ();
  return 0;
}

At -O2 GCC 17 aborts, GCC 16.1 does not. GCC 17 with -O1 or -O2
-fno-tree-forwprop are fine.

Compiler Explorer, -O2 (GCC 16.1 vs. GCC 17):

x86-64 GCC 17.0.0 20261023, -O2:
"a":
        mov     rax, rdi
        mul     rsi
        mov     rax, rdx
        ret
"b":
        mov     rax, rdi
        mul     rsi
        mov     rax, rdx
        ret

Possibly related to r17-3440-g91cca3ac45e7 ("forwprop: Add long-multiply ladder
variants", PR107090).

Reply via email to