Home >Backend Development >C++ >Do C/C Compilers Utilize Push/Pop Instructions for Local Variable Creation?
Traditional C/C compilers create local variables by increasing the stack pointer (ESP) once. However, using push/pop instructions can result in more compact and potentially faster code. This article explores which compilers offer this optimization.
Research indicates that all four major x86 compilers (GCC, Clang, ICC, and MSVC) do not currently use push/pop instructions to create local variables. Instead, they prefer a variation of the older method, using sub rsp, c to decrease the stack pointer.
Using push/pop for local variables offers several advantages:
While push/pop can be beneficial, it's not recommended in all cases. For example, it can lead to extra stack-sync uops when mixed with [rsp x] addressing modes.
Consider the following function:
int extfunc(int *,int *); void foo() { int a=1, b=2; extfunc(&a, &b); }
Improved compilation using push/pop:
# compiled for the x86-64 System V calling convention: # integer args in rdi, rsi (,rdx, rcx, r8, r9) push 2 # only 2 bytes lea rdi, [rsp + 4] mov dword ptr [rdi], 1 mov rsi, rsp # special case for lea rsi, [rsp + 0] call extfunc(int*, int*) pop rax # alternative to add rsp,8 ret
Note: This example optimizes for compactness and speed by overlapping the last 4 bytes of the push instruction with the first 4 bytes of the lea instruction.
While push/pop instructions can offer code-size and performance advantages for creating local variables, they are not currently utilized by mainstream C/C compilers. This is likely due to the potential for extra stack-sync uops and the complexities in managing stack offsets. However, as hardware and compiler optimizations evolve, it's possible that push/pop for local variables may become more widely adopted in the future.
The above is the detailed content of Do C/C Compilers Utilize Push/Pop Instructions for Local Variable Creation?. For more information, please follow other related articles on the PHP Chinese website!