Home >Backend Development >C++ >Do C/C Compilers Utilize Push/Pop Instructions for Local Variable Creation?

Do C/C Compilers Utilize Push/Pop Instructions for Local Variable Creation?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-09 11:58:10244browse

Do C/C   Compilers Utilize Push/Pop Instructions for Local Variable Creation?

C/C Compilers with Push/Pop Instructions for Local Variables

Introduction

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.

Compiler Support

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.

Advantages of Push/Pop

Using push/pop for local variables offers several advantages:

  • Code-size reduction: Push instructions are only 2 bytes compared to 9-11 bytes for sub/mov pairs.
  • Potential performance improvement: On CPUs with a stack engine, push operations may incur fewer stack-sync uops than sub rsp, making it faster in certain scenarios.

When to Avoid Push/Pop

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.

Practical Use Case

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.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn