为什么std :: pair <long,long>和std :: tuple <long,long>生成不同的代码

时间:2019-06-09 16:18:30

标签: c++ assembly x86-64 abi

我期望std :: pair和std :: tuple具有类似的行为。但事实证明,与std :: pair相比,std :: tuple生成更差的asm代码。

https://gcc.godbolt.org/z/Ri4M8z-每晚gcc 10.0.0构建,-O3 -std=c++17
为x86-64 System V ABI进行编译

#include <utility>

std::pair<long, long> test_pair() {
    return { 1, 2 };
}
# returned in RDX:RAX
test_pair():
        mov     eax, 1
        mov     edx, 2
        ret

#include <tuple>
std::tuple<long, long> test_tuple() {
    return { 1, 2 };
}
# returned via hidden pointer, not packed into registers
test_tuple():
        mov     QWORD PTR [rdi], 2
        mov     QWORD PTR [rdi+8], 1
        mov     rax, rdi
        ret

这两个函数都返回两个值。 test_pair使用寄存器来存储期望的值;但是test_tuple将值存储在堆栈中,这似乎是未优化的。为什么这两个函数的行为不同?

0 个答案:

没有答案