Aug 24, 2018

[gcc] beware of returning none-integer from argument as r-value struct argument.

https://www.reddit.com/r/cpp/comments/99e1ri/interesting_gcc_optimizer_bug/

Should use RVO however gcc7 -O3 failed to use RVO but create temporary value on stack then assigned to implicit argument(i.e rax, rdx)

Could directy assign value to rax,rdx (RVO) instead.


code:
https://godbolt.org/z/A2Kpkl


struct fun {
    // fun(const int &a, const int &b) {}
    long a;
    long b;
};

fun run(long a)
{

    return {a, a};
}

Assembly gcc7 -O3:
        mov     QWORD PTR [rsp-24], rdi
        movq    xmm0, QWORD PTR [rsp-24]
        punpcklqdq      xmm0, xmm0
        movaps  XMMWORD PTR [rsp-24], xmm0
        mov     rax, QWORD PTR [rsp-24]
        mov     rdx, QWORD PTR [rsp-16]
        ret
Assembly clang6 -O3:
run(long):                                # @run(long)
        mov     rax, rdi
        mov     rdx, rdi
        ret

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.