[llvm-bugs] [Bug 52365] New: missed optimization for the sum of two numbers

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Oct 30 08:24:15 PDT 2021


            Bug ID: 52365
           Summary: missed optimization for the sum of two numbers
           Product: clang
           Version: 12.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dushistov at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

For such simple function:

int32_t f(int32_t & __restrict a, const int32_t & __restrict b) {
    a += b + 17;
    return a + b;

clang (12/13) produces (-O3) such assembly (amd64):

        mov     eax, dword ptr [rsi]
        mov     ecx, dword ptr [rdi]
        lea     edx, [rax + rcx]
        add     ecx, eax
        add     ecx, 17
        mov     dword ptr [rdi], ecx
        add     eax, edx
        add     eax, 17

while gcc producues:

        mov     eax, DWORD PTR [rsi]
        mov     edx, DWORD PTR [rdi]
        lea     edx, [rax+17+rdx]
        mov     DWORD PTR [rdi], edx
        add     eax, edx

even without benchmark (which show that gcc's variant wins)
you can see that for some reason clang forgot that in "ecx"
has "a + b + 17", so it can just add it to eax and that's all.
But for some reason it recalculate expression again via:
        add     eax, edx
        add     eax, 17

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211030/2ea4ee41/attachment.html>

More information about the llvm-bugs mailing list