眠りネ*_*ネロク 2 optimization assembly gcc x86-64 alloca
考虑以下玩具示例,该示例通过以下alloca()函数在堆栈上分配内存:
#include <alloca.h>
void foo() {
volatile int *p = alloca(4);
*p = 7;
}
Run Code Online (Sandbox Code Playgroud)
使用gcc 8.2编译上面的函数-O3,得到以下汇编代码:
foo:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
leaq 15(%rsp), %rax
andq $-16, %rax
movl $7, (%rax)
leave
ret
Run Code Online (Sandbox Code Playgroud)
老实说,我本来期望更紧凑的汇编代码.
andq $-16, %rax上面代码中的指令导致在地址和(包括两者)之间rax包含(仅)16字节对齐的地址.rsprsp + 15
这种对齐强制执行是我不明白的第一件事:为什么alloca()将分配的内存与16字节边界对齐?
无论如何,让我们考虑我们希望分配的内存alloca()是16字节对齐的.即便如此,在上面的汇编代码中,请记住GCC假设堆栈在执行函数调用时(即call foo)执行16字节边界,如果我们注意内部堆栈的状态foo() 在推送rbp寄存器之后:
Size Stack RSP mod 16 Description
-----------------------------------------------------------------------------------
------------------
| . |
| . |
| . |
------------------........0 at "call foo" (stack 16-byte aligned)
8 bytes | return address |
------------------........8 at foo entry
8 bytes | saved RBP |
------------------........0 <----- RSP is 16-byte aligned!!!
Run Code Online (Sandbox Code Playgroud)
我认为通过利用红区(即无需修改rsp)和rsp已包含16字节对齐地址的事实,可以使用以下代码:
foo:
pushq %rbp
movq %rsp, %rbp
movl $7, -16(%rbp)
leave
ret
Run Code Online (Sandbox Code Playgroud)
寄存器中包含的地址rbp是16字节对齐的,因此rbp - 16也将与16字节边界对齐.
更好的是,新的堆栈帧的创建可以被优化掉,因为rsp没有修改:
foo:
movl $7, -8(%rsp)
ret
Run Code Online (Sandbox Code Playgroud)
这只是一个错过的优化还是我在这里错过了其他的东西?
这是(部分)错过了gcc中的优化.Clang按预期做到了.
我部分说过,因为如果你知道你将使用gcc,你可以使用内置函数(使用gcc和其他编译器的条件编译来获得可移植代码).
__builtin_alloca_with_align是你的朋友;)
下面是一个示例(已更改,因此编译器不会将函数调用减少为单个ret):
#include <alloca.h>
volatile int* p;
void foo()
{
p = alloca(4) ;
*p = 7;
}
void zoo()
{
// aligment is 16 bits, not bytes
p = __builtin_alloca_with_align(4,16) ;
*p = 7;
}
int main()
{
foo();
zoo();
}
Run Code Online (Sandbox Code Playgroud)
反汇编代码(带objdump -d -w --insn-width=12 -M intel)
Clang将生成以下代码(clang -O3 test.c) - 两个函数看起来都很相似
0000000000400480 <foo>:
400480: 48 8d 44 24 f8 lea rax,[rsp-0x8]
400485: 48 89 05 a4 0b 20 00 mov QWORD PTR [rip+0x200ba4],rax # 601030 <p>
40048c: c7 44 24 f8 07 00 00 00 mov DWORD PTR [rsp-0x8],0x7
400494: c3 ret
00000000004004a0 <zoo>:
4004a0: 48 8d 44 24 fc lea rax,[rsp-0x4]
4004a5: 48 89 05 84 0b 20 00 mov QWORD PTR [rip+0x200b84],rax # 601030 <p>
4004ac: c7 44 24 fc 07 00 00 00 mov DWORD PTR [rsp-0x4],0x7
4004b4: c3 ret
Run Code Online (Sandbox Code Playgroud)
GCC这个(gcc -g -O3 -fno-stack-protector)
0000000000000620 <foo>:
620: 55 push rbp
621: 48 89 e5 mov rbp,rsp
624: 48 83 ec 20 sub rsp,0x20
628: 48 8d 44 24 0f lea rax,[rsp+0xf]
62d: 48 83 e0 f0 and rax,0xfffffffffffffff0
631: 48 89 05 e0 09 20 00 mov QWORD PTR [rip+0x2009e0],rax # 201018 <p>
638: c7 00 07 00 00 00 mov DWORD PTR [rax],0x7
63e: c9 leave
63f: c3 ret
0000000000000640 <zoo>:
640: 48 8d 44 24 fc lea rax,[rsp-0x4]
645: c7 44 24 fc 07 00 00 00 mov DWORD PTR [rsp-0x4],0x7
64d: 48 89 05 c4 09 20 00 mov QWORD PTR [rip+0x2009c4],rax # 201018 <p>
654: c3 ret
Run Code Online (Sandbox Code Playgroud)
正如您所见,动物园现在看起来像预期的,类似于铿锵代码.