(Using Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.6.0)
When disassembling some code compiled with -O2, I noticed that a lot of them have a seemingly unnecessary saving and restoring of the base pointer rbp, usually looking like the following
pushq %rbp movq %rsp, %rbp ... popq %rbp I know what this would be for, but it seems to be used even in situations where it seems completely unnecessary, such as in the following disassembled convoluted identity function emited by objdump
__Z8identityI5arrayIiLm2EEET_S2_: 60: 55 pushq %rbp 61: 48 89 e5 movq %rsp, %rbp 64: 48 89 f8 movq %rdi, %rax 67: 5d popq %rbp 68: c3 retq 69: 0f 1f 80 00 00 00 00 nopl (%rax) where the only two meaningful instructions are the move from rdi to rax (first argument to return register) and the obviously necessary retq (I assume the nopl is for padding or alignment for whatever follows).
Is there a reason for this seemingly unnecessary context save?
-fomit-frame-pointerand see if the boilerplate code goes away. (Or it may just be that you have-fno-omit-frame-pointerin your build settings.)-O2should already enable-fomit-frame-pointeron any recent gcc version, but maybe not on clang.-fomit-frame-pointerwhen compiling for x86-64 at-O2and above. I'm wondering if OP's code was compiled with-fno-omit-frame-pointer?-fomit-frame-pointer. This is a leaf function that doesn't throw. No unwinding should be initiated from this function; the only exit path is theretqinstruction. A profiling tool based on interrupting the code with a timer might have a hard time without the frame pointers. In the case of this type of leaf function, the tool will just see the original frame, since%rbphasn't moved, so there is no real problem.