Re: Merits of callee-saved registers



On Tue, 22 Apr 2008 09:51:56 +0000, Tim Frink wrote:

Hi,

I've a general question about calling conventions and the use of
registers across calls. In a book I found this: "For a variable whose
range of appearance spans many procedure calls, the use of a
callee-saved register is advantageous, because saving and restoring once
at the procedure entry and exit respectively are cheaper than saving and
restoring around each call."

What I don't understand is the term "cheaper". In my opinion cheaper
must mean less code (i.e. less instructions in the code) since when the
same function is invoked multiple times, there are just once the
instructions in this function for saving/restoring while in the case of
register saving/restoring around the each call for each call separate
instructions must be added.

Cheaper cannot mean faster execution because the number of executed
saving/restoring instructions would be the same for both approaches,
right?

If the callee saves the registers, it only needs to save the ones it
modifies. If the caller saves them, it has no reliable way of knowing
what registers the callee will modify, so it is forced to save all of the
registers that the callee could possibly modify.

It's a matter of having enough knowledge to know what to save and what
not to.

Also, it implies the code is somewhat smaller. take, for example, the
pseudo-asm below:

func:
save_regs
; do stuff
restore regs
ret

caller:
push args
call func
push args
call func

You only have save_regs and restore_regs in 2 places. Compare to:

func:
; do stuff
ret

caller:
push args
save_regs
call func
restore_regs
push args
save_regs
call func
restore_regs

The code is longer, as the caller has to wrap _both_ calls with a save/
restore sequence.

.


Quantcast