Assembled c++ appears to contain superfluous instructions

前端 未结 2 662
庸人自扰
庸人自扰 2021-01-18 02:26

I have a cpp file containing only the following:

void f(int* const x)
{
  (*x)*= 2;
}

I compile with:

g++ -S -masm=intel -O         


        
相关标签:
2条回答
  • 2021-01-18 02:37

    Those instructions are used to access the stack in function bodies. It is a generalization and used for debugging purposes.

    0 讨论(0)
  • 2021-01-18 02:53

    CLANG/CLANG++ is both a native compiler and a cross compiler that supports multiple targets. On OS/X the targets by default are usually a variant of x86_64-apple-darwin for 64-bit code and i386-apple-darwin for 32-bit code. The code you are seeing that resembles this form:

    push    rbp
    mov rbp, rsp
    
    [snip]
    
    pop rbp
    ret
    

    Is produced to introduce stack frames. By default CLANG++ implicitly enables stack frames for the Apple Darwin targets. This differs from the Linux targets like x86_64-linux-gnu and i386-linux-gnu. Stack frames can come in handy for some profiling and unwind libraries and can aid debugging on the OS/X platforms which is why I believe they opt to turn them on by default.

    You can explicitly omit frame pointers with CLANG++ using the option -fomit-frame-pointer. If you use the build command

    g++ -S -masm=intel -O3 -fno-exceptions -fno-asynchronous-unwind-tables \
        -fomit-frame-pointer f.cpp 
    

    The output would be something similar to:

        shl     dword ptr [rdi]
        ret
    

    A Look at Code with Different Targets

    If you use different targets with CLANG++ you'd discover the behavior is different. This is an x86-64 Linux target where we don't explicitly omit the frame pointer:

    clang++ -target x86_64-linux-gnu -S -masm=intel -O3 -fno-exceptions \
        -fno-asynchronous-unwind-tables f.cpp 
    

    Which generates:

        shl     dword ptr [rdi]
        ret
    

    This is your original x86-64 Apple Darwin target:

    clang++ -target x86_64-apple-darwin -S -masm=intel -O3 -fno-exceptions \
        -fno-asynchronous-unwind-tables f.cpp 
    

    Which generates:

        push    rbp
        mov     rbp, rsp
        shl     dword ptr [rdi]
        pop     rbp
        ret
    

    And then the x86-64 Apple target with frame pointers omitted:

    clang++ -target x86_64-apple-darwin -S -masm=intel -O3 -fno-exceptions \
        -fno-asynchronous-unwind-tables -fomit-frame-pointer f.cpp 
    

    Which generates:

        shl     dword ptr [rdi]
        ret
    

    You can do a comparison of these targets on Godbolt. The first column of generated code is similar to the question - Apple target with implicit frame pointers. The second is Apple target without frame pointers and the third is an x86-64 Linux target.

    0 讨论(0)
提交回复
热议问题