Register spilling is a critical technique in compiler design where variables temporarily stored in CPU registers are moved ("spilled") to memory when the compiler runs out of available registers. This process represents a fundamental trade-off between computation speed and resource constraints.
Why Register Spilling Occurs
Modern CPUs have a limited number of registers (typically 8-32 general-purpose registers depending on the architecture). These registers are the fastest storage location for variables during program execution, with access times 10-100x faster than even L1 cache. However, when a program section needs more variables than available registers, some must be stored elsewhere.
The Register Allocation Problem
The compiler's register allocator faces the challenging task of:
Determining which variables should occupy registers at any given time
Deciding which registers to "spill" to memory when needed
Scheduling when to reload spilled values back into registers
The Spilling Process
When spilling occurs, the compiler:
Identifies candidates for spilling based on various heuristics
Generates code to store register values to stack memory locations
Frees up the register for other variables
Generates code to reload the value later when needed
Updates the register allocation table to track where values reside
For example, consider this code with high register pressure:
c
int compute(int a, int b, int c, int d, int e, int f, int g, int h) {
int x = a * b;
int y = c * d;
int z = e * f;
int w = g * h;
return (x + y) * (z + w);
}
On a machine with just 4 registers, the compiler might generate:
assembly
# Assume parameters in memory locations [a-h]
load r1, [a] # Load a into r1
load r2, [b] # Load b into r2
mul r1, r1, r2 # r1 = a*b (value x)
store r1, [temp1] # Spill x to memory to free r1
load r1, [c] # Load c into r1
load r2, [d] # Load d into r2
mul r1, r1, r2 # r1 = c*d (value y)
store r1, [temp2] # Spill y to memory
# ... similar for z and w ...
load r1, [temp1] # Reload x
load r2, [temp2] # Reload y
add r1, r1, r2 # r1 = x+y
load r2, [temp3] # Reload z
load r3, [temp4] # Reload w
add r2, r2, r3 # r2 = z+w
mul r1, r1, r2 # r1 = (x+y)*(z+w)
Spilling Strategies
Compilers use various heuristics to determine which registers to spill:
Liveness Analysis: Variables with the longest time until next use are preferred spill candidates
Usage Frequency: Less frequently used variables are spilled first
Spill Cost: Variables that are expensive to reload (e.g., need recomputation) are less desirable to spill
Loop Depth: Variables in inner loops are kept in registers when possible
Additional instructions consume instruction cache space
Code with frequent spilling can suffer 2-10x performance degradation
Optimization Techniques to Reduce Spilling
Compilers employ various strategies to minimize register spilling:
Graph Coloring Allocation: Modeling register allocation as a graph coloring problem
Live Range Splitting: Breaking variable lifetimes into smaller segments
Instruction Scheduling: Reordering instructions to reduce the number of simultaneously live variables
Register Coalescing: Combining compatible variables to share registers
Rematerialization: Recomputing values instead of spilling and reloading them
Real-World Considerations
In practice, register spilling is most problematic in:
Heavily optimized numerical code with many intermediate values
Deep call chains where each function needs its own registers
Functions with complex control flow requiring preservation of many values
Code targeting architectures with few registers (e.g., x86 vs. ARM64)
Developers working on performance-critical code sometimes explicitly help the compiler by breaking complex expressions into smaller parts, allowing the compiler to better manage register usage and minimize spilling.