From: Tim Frink on
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?

Or do you understand this in another way?

Regards,
Tim
From: Robert Redelmeier on
Tim Frink <plfriko(a)yahoo.de> wrote in part:
> 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."

I'm a native english speaker and I don't understand either.
Cheaper can mean shorter or quicker (cheaper on cycles)
which are often the same for once-thru code.

To me, register preservation is a matter of conventions
without any ideal answer: only the caller knows which regs
need saving, and only the callee knows which registers will
be trampled. Often, no regs need saving.

In x86, the arch is allegedly short of registers so the
usual presumption is all will get trampled. This leads to
caller-save conventions. But note that ESP and EBP usually
are callee-save . Since procedures have non-zero overhead,
they should be reserved for "significant" code that makes
the trampling more likely.

-- Robert

From: robertwessel2 on
On Apr 22, 4:51 am, Tim Frink <plfr...(a)yahoo.de> 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?
>
> Or do you understand this in another way?


The wording is particularly unclear, but the advantage of using a
callee save register in that case is that some (or many) of the called
routines might avoid using that register at all, and thus will not
have to save it. So instead of saving it around each call, it gets
saved only inside those calls that need the register. So you save
both static and dynamic instructions.

Now the tradeoff is not necessarily that simple - a register might not
need saving (just reloading), so it might well be better to not use a
callee save register and always just generate the reload.

The whole caller/callee save thing is an attempt at allowing
reasonable performance while maintaining a consistent convention.
Better compilers (or assembler programmers), often avoid using a fixed
division like that, except in external calls where the convention is
required.