The caller-saved / callee-saved terminology is based on a simplistic model of register variables where callers actually do save/restore (back into the same register) around each call for any call-clobbered registers where they were keeping a value they want later. (Instead of keeping long-term-useful values elsewhere, or starting fresh with register allocation to reload as needed.)
You have to understand that "caller-saved" means "saved somehow if you want the value later". Not that all registers are always saved/restored around every function call by either the caller or callee.
In reality, efficient code lets values get destroyed when they're no longer needed. Compilers typically make functions that save a few call-preserved registers at the start (prologue) of a function (and restore them at the end, in the epilogue). Inside the function, they use those regs for values that need to survive across function calls. (If they run out of call-preserved registers, they might indeed need to spill something before a call, but it often doesn't need to get reloaded again right away, maybe not until a few calls later, or after a loop, and maybe not necessarily into the same register it was in before.)
I prefer "call-preserved" vs. "call-clobbered", which are unambiguous and self-describing once you've heard of the basic concept, and don't require any serious mental gymnastics to think about from the caller's perspective or the callee's perspective. (Both terms are from the same perspective.)
Plus, these terms differ by more than one letter.
The terms volatile / non-volatile are pretty good, by analogy with storage which loses its value on power-loss or not, (like DRAM vs. Flash). But the C volatile
keyword has a totally different technical meaning, so that's a downside to "(non)-volatile" when describing C calling conventions.
- Call-clobbered, aka caller-saved or volatile registers are good for scratch / temporary values that aren't needed after the next function call.
From the callee's perspective, your function can freely overwrite (aka clobber) these registers without saving/restoring.
From a caller's perspective, call foo
destroys (aka clobbers) all the call-clobbered registers, or at least you have to assume it does.
You can write private helper functions that have a custom calling convention, e.g. you know they don't modify a certain register. But if all you know (or want to assume or depend on) is that the target function follows the normal calling convention, then you have to treat a function call as if it does destroy all the call-clobbered registers. That's literally what the name comes from: a call clobbers those registers.
Some compilers that do inter-procedural optimization can also create internal-use-only definitions of functions that don't follow the ABI, using a custom calling convention.
- Call-preserved, aka callee-saved or non-volatile registers keep their values across function calls. This is useful for loop variables in a loop that makes function calls, or basically anything in a non-leaf function in general.
From a callee's perspective, these registers can't be modified unless you save the original value somewhere so you can restore it before returning. Or for registers like the stack pointer (which is almost always call-preserved), you can subtract a known offset and add it back again before returning, instead of actually saving the old value anywhere. I.e. you can restore it by dead reckoning, unless you allocate a runtime-variable amount of stack space. Then typically you restore the stack pointer from another register.
A function that can benefit from using a lot of registers can save/restore some call-preserved registers just so it can use them as more temporaries, even if it doesn't make any function calls. Normally you'd only do this after running out of call-clobbered registers to use, because save/restore typically costs a push/pop at the start/end of the function. (Or if your function has multiple exit paths, a pop
in each of them.)
The name "caller-saved" is misleading: you don't have to specifically save/restore them. Normally you arrange your code to have values that need to survive a function call in call-preserved registers, or somewhere on the stack, or somewhere else that you can reload from. It's normal to let a call
destroy temporary values.
An ABI or calling convention defines which are which
See for example What registers are preserved through a linux x86-64 function call for the x86-64 System V ABI.
Also, arg-passing registers are always call-clobbered in all function-calling conventions I'm aware of. See Are rdi and rsi caller saved or callee saved registers?
But system-call calling conventions typically make all the registers except the return value call-preserved. (Usually including even condition-codes / flags.) See What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64