Optimization issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I think I found a small optimization issue by mistake while checking
the generated assembly code for a function that was using `memcpy`.

Here's the smallest example showing the issue: https://godbolt.org/g/xTn4bZ

When I put a 1 (instead of sizeof int) on the third parameter of
std::memcpy() the resulting code stores the byte on the stack and then
reads it back before returning from the function, (clang doesn't seem
to have the issue)

int read(const void* buffer)
{
    int r;
    std::memcpy(&r, buffer, 1);
    // std::memcpy(&r, buffer, sizeof(r));
    return r;
}

This generates the following code (GCC 7.2):

read(void const*):
    movzx eax, BYTE PTR [rdi]
    mov BYTE PTR [rsp-4], al
    mov eax, DWORD PTR [rsp-4]
    ret

While clang 5.0.0 generates the following:

read(void const*):
    movzx eax, byte ptr [rdi]
    ret

(Both compiled with -O3)


Do you think there's a reason for this or is this just an optimizer bug?

Regards,
Juan.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux