This is an excellent and insightful question — you’re right that textbooks often present calling conventions as “just the way it is,” without explaining the engineering trade-offs behind them. The reason calling conventions use a mixture of caller-saved (also called volatile or call-clobbered) and callee-saved (non-volatile or call-preserved) registers is to strike an optimal balance between performance, code size, and register pressure, depending on usage patterns in real programs.
Let’s break down why a mixed strategy is better than making all registers one type or the other.
🚫 Why NOT all caller-saved?
If all registers were caller-saved, then:
- Every function call would force the caller to save every live register it cares about before the call, even if the callee doesn’t touch most of them.
- This leads to excessive saving/restoring overhead in the caller — especially for functions that don’t actually clobber many registers.
- Code bloat: You’d have lots of redundant
push/pop or mov instructions around every function call.
- Performance penalty: Memory traffic increases unnecessarily.
Example: Imagine a small utility function like strlen() that only uses 2–3 registers internally. If all 16 general-purpose registers are caller-saved, the caller must save/restore up to 15 registers just in case — even though strlen() barely touches any. That’s wasteful.
🚫 Why NOT all callee-saved?
If all registers were callee-saved, then:
- Every function must save and restore every register it uses, even if the caller doesn’t care about their values.
- Functions that use many registers (e.g., loops, math routines) would incur heavy prologue/epilogue costs.
- Wasted effort: A leaf function might use 8 temporary registers but have no callers relying on those values — yet it still saves/restores all 8.
- Stack pressure and memory traffic increase unnecessarily.
Example: A function doing a tight loop with 6 temporary counters. If all registers are callee-saved, it must push/pop 6 registers — even if none of its callers cared about those values. Again, wasteful.
✅ Why MIXED is optimal
The mixed convention exploits statistical usage patterns observed in real code:
➤ Caller-saved registers are good for:
- Temporary values that aren’t needed across function calls.
- Values used in short-lived computations.
- Situations where the caller knows it won’t need the value after a call → no need to save.
- Common in expressions, intermediate calculations.
→ So if a register holds something transient, let the caller decide whether to preserve it. If not needed, no cost!
➤ Callee-saved registers are good for:
- Long-lived values that survive across multiple function calls (e.g., loop counters, object pointers, accumulators).
- Values expensive to recompute.
- Situations where many callers might rely on the same register → better to save once in the callee than force every caller to save it.
→ If a function is called frequently and preserves a commonly-used register, it amortizes the save/restore cost across many callers.
📊 Analogy: Renting vs Owning Tools
Think of registers like tools:
- Caller-saved = rented tools: You only pay (save/restore) if you need them to survive a job (function call). Otherwise, you let them go.
- Callee-saved = owned tools: The function guarantees to return them in the same state. Useful if many people (callers) expect the tool to remain unchanged.
A smart workshop doesn’t rent all tools (too costly per job), nor does it require every worker to own every tool (wasteful). It finds a balance: some core tools owned (callee-saved), others rented as needed (caller-saved).
💡 Real-world design insight
Compiler writers and ISA designers analyze typical programs and ask:
“Which registers are most often live across function calls? Which are typically temporary?”
They assign the former as callee-saved, the latter as caller-saved.
For example, in x86-64 System V ABI:
-
%rbx, %rbp, %r12–%r15 → callee-saved
(often used for base pointers, long-lived variables)
-
%rax, %rcx, %rdx, %rsi, %rdi, %r8–%r11 → caller-saved
(used for temporaries, arguments, return values)
This assignment reflects common usage patterns that minimize overall save/restore traffic.
🧠 Summary: The “Why” in One Sentence
Mixed conventions minimize total save/restore overhead by assigning preservation responsibility to whichever side — caller or callee — is statistically more likely to benefit from or be burdened by it, based on typical program behavior.
It’s a classic systems optimization: avoid forcing unnecessary work on either side, and let each handle what they’re best positioned to manage.
You’ve touched on a deep principle here: good low-level interfaces (like ABIs) aren’t arbitrary — they’re carefully engineered responses to observed workload characteristics. The mixture isn’t confusion — it’s optimization. 😊