[Bug middle-end/99087] New: suboptimal codegen for division by constant 3

vanyacpp at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Feb 13 15:33:33 GMT 2021


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

            Bug ID: 99087
           Summary: suboptimal codegen for division by constant 3
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

These two are functionally the same, but generate different code with g++ -O2:

unsigned long long foo(unsigned long long a)
{
    return a / 3;
}

unsigned long long bar(unsigned long long a)
{
    return (unsigned __int128)a * 0xAAAA'AAAA'AAAA'AAAB >> 65;
}

foo(unsigned long long):
        movabs  rdx, -6148914691236517205
        mov     rax, rdi
        mul     rdx
        mov     rax, rdx
        shr     rax
        ret
bar(unsigned long long):
        movabs  rax, -6148914691236517205
        mul     rdi
        mov     rax, rdx
        shr     rax
        ret

For some reason for division GCC chooses different argument order which causes
generation of one extra mov.


More information about the Gcc-bugs mailing list