(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?