So I'm reading through "Modern compiler Implementation in C" and in one of the problems it presents the following C function
int f(int a) {int b; b = a+1; g(); h(b); return b+2;}
And it poses the question: "If local variable b is live across more than one procedure call is it kept in a callee-save register? Explain how doing this would speed up the program
After writing functions for f, g, and h compiling / disassembling it with gcc -c -S simple-functions.c I get the output:
f:
.LFB3:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $24, %rsp
movl %edi, -20(%rbp) # Receive input 'a' in the edi register. Save it to the stack as a local variable 'b = a'
movl -20(%rbp), %eax # Move 'b' into a register and increment it
addl $1, %eax
movl %eax, -4(%rbp) # Save the value of 'b' to the stack
movl $0, %eax
call g
movl -4(%rbp), %eax # Reclaim the saved value of b
movl %eax, %edi # Pass b to the function 'h' through the edi register
call h
movl -4(%rbp), %eax # Reclaim b again for return value
addl $2, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
(Where I have included annotations based on my interpretation of what the code is doing)
Looking at the above code it seems to me the register being used to store b, 'eax', is a a "caller-save" register, as the caller 'f' is saving its value before calling g.
But now I'm confused about the question " Explain how doing this would speed up the program". It doesn't seem to me that putting b in a callee-save register would speed things up. In fact it seems like it would be slower. In the above code 'b' is saved to the stack once but reclaimed twice. If 'b' were in a callee-save register both the 'g' and 'h' function would need to go through the process of saving and then restoring the value 'eax' twice so it would actually be doing more.
EDIT: My understanding is that it is currently doing 1 write and 2 reads. But using a callee-save would cause it to do 2 writes and 2 reads as both f and g would save and restore the register.
Is what I'm saying making sense. Can someone explain how using a callee-save register would be advantageous in this situation?