Posted on

In a function prologue it’s considered polite (and/or required by ABI) to stash registers and then restore them in the epilogue, so that callers don’t need to do that themselves. The alternative is that all callers stash the registers, and presumably (if each function is called one or more times) then there are many more function calls than function definitions, so having the function do this work is the correct approach to minimise binary size. In scenarios where functions are visible across multiple translation units this is basically the only option.

However within a single translation unit, it would be possible to analyse what registers a function uses, what registers the caller uses, and determine whether it would be better to instead stash only the minimum number of registers at every caller.