CSE411

CSE411: Introduction to Compilers

Register Allocation

Jooyong Yi (UNIST)

2026 Fall
CSE411

Register Allocation Problem

  • The problem: IR and its subsequent MIR can use an unlimited number of virtual registers, but the machine has only a limited number of physical registers.
  • Naive solution: Map each virtual register to distinct a memory location.
    • However, this incurs excessive memory accesses and performance slowdown.
  • Goal: Rewrite the MIR to make effective use of physical registers while minimizing memory accesses.
2026 Fall
CSE411

Register Allocation Problem Example

v3 = v1 + v2
v4 = v1 + v3
ret v4
  • Assume we have only two physical registers, r1 and r2.
2026 Fall
CSE411

Register Allocation Problem Example

  v1, v4 ---------  r1       

  v2, v3 ---------  r2
v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
2026 Fall
CSE411

How to Solve the Register Allocation Problem?

  v1, v4 ---------  r1       
  
  v2, v3 ---------  r2
v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
2026 Fall
CSE411

Observations

v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
  • v1 and v2 are used at the same time.
    • We say that v1 and v2 interfere with each other.
  • Thus, reg(v1) reg(v2)
2026 Fall
CSE411

Observations

v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
  • v1 and v2 interfere with each other. Thus, reg(v1) reg(v2)
    v1 ------ v2


    v3        v4
2026 Fall
CSE411

Observations

v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
  • v1 and v2 interfere with each other. Thus, reg(v1) reg(v2)
  • v1 and v3 interfere with each other. Thus, reg(v1) reg(v3)
    v1 ------ v2
    | 
    |
    v3        v4
2026 Fall
CSE411

Interference Graph

    v1 ------ v2
    | 
    |
    v3        v4
  • Nodes: variables
  • Each edge connects variables interfering with each other.
2026 Fall
CSE411

Solving Register Allocation Problem with Interference Graph

  • Assume we have only two physical registers, r1 and r2.
    v1: __  ------ v2: __
    | 
    |
    v3: __         v4: __
2026 Fall
CSE411

Solving Register Allocation Problem with Interference Graph

    v1: r1  ------ v2: r2
    | 
    |
    v3: r2         v4: r1 or r2
2026 Fall
CSE411

Recall the Solution

    v1:r1  ------ v2: r2
    | 
    |
    v3:r2         v4: r1 or r2
  v1, v4 ---------  r1       
  
  v2, v3 ---------  r2
v3 = v1 + v2  --->   r2 = r1 + r2
v4 = v1 + v3         r1 = r1 + r2 
ret v4               ret r1
2026 Fall
CSE411

General Strategy

  • Two key questions
    • Q1: How to construct an interference graph?
    • Q2: How to assign the registers to the nodes in the interference graph?
2026 Fall
CSE411

Q1: How to construct an interference graph?

  • Q1-1: When do two variables interfere with each other?
    • Two variables interfere if they are live at the same program point.
2026 Fall
CSE411

DFA for Live Variables

  • Information of interest: Set of live variables

  • Transfer function

    • IN[B] = (OUT[B] - DEF[B]) ∪ USE[B]
      • DEF[B]: Variables defined in B.
      • USE[B]: Variables used in B before being defined in B.
  • Join function

    • OUT[B] =
2026 Fall
CSE411

Our Running Example

                 Live Vars
                 
v3 = v1 + v2  
                 
v4 = v1 + v3  
                 
ret v4        
2026 Fall
CSE411

Our Running Example

                 Live Vars
                 {v1, v2}
v3 = v1 + v2  
                 {v1, v3}
v4 = v1 + v3  
                 {v4}
ret v4        
2026 Fall
CSE411

Our Running Example

                 Live Vars
                 {v1, v2}
v3 = v1 + v2  
                 {v1, v3}
v4 = v1 + v3  
                 {v4}
ret v4        
    v1  ------ v2
    | 
    |
    v3         
2026 Fall
CSE411

Q2: How to assign the registers to the nodes in the interference graph?

2026 Fall
CSE411

Q2: How to assign the registers to the nodes in the interference graph?

2026 Fall
CSE411

Graph Coloring Problem

  • Adjacent nodes must have different colors.
2026 Fall
CSE411

Graph Coloring Problem

  • NP-complete problem
  • Still, there are efficient algorithms that work well in special cases.
2026 Fall
CSE411

Idea

  • Assume that we have 3 colors.
2026 Fall
CSE411

Idea

  • Assume that we have 3 colors.
  • If we can successfully color , we can color .
2026 Fall
CSE411

Coloring by Simplification

  • Input: Graph and the number of colors
  • Algorithm
    • Find a node with fewer than neighbors.
    • Remove and its edges from . i.e.,
    • Recursively solve the problem with .
2026 Fall
CSE411

Simplification Example ()

2026 Fall
CSE411

Simplification Example ()

  • Stack: 5
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2 1
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2 1 3
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2 1 3
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2 1
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5 2
2026 Fall
CSE411

Simplification Example ()

  • Stack: 5
2026 Fall
CSE411

Simplification Example ()

  • Stack:
2026 Fall
CSE411

Another Example ()

2026 Fall
CSE411

Another Example ()

2026 Fall
CSE411

Register Spilling Example

                     Live Variables
v1 = a + 1
v2 = b + 2
v3 = a * b
v4 = v1 + v2
v5 = v3 + v4
ret v5
2026 Fall
CSE411

Register Spilling Example

                     Live Variables
v1 = a + 1           {a, b, v1}
v2 = b + 2           {a, b, v1, v2}
v3 = a * b           {v1, v2, v3}
v4 = v1 + v2         {v3, v4}
v5 = v3 + v4         {v5}
ret v5               {}
  • Assume we have three physical registers.
2026 Fall
CSE411

Register Spilling Example

  • Suppose we spill v1.
v1 = a + 1
v2 = b + 2
v3 = a * b      ---->
v4 = v1 + v2
v5 = v3 + v4
ret v5
2026 Fall
CSE411

Register Spilling Example

  • Suppose we spill v1.
v1 = a + 1              v1 = a + 1
                        store v1, [slot]
v2 = b + 2              v2 = b + 2
v3 = a * b      ---->   v3 = a * b
                        t1 = load [slot]
v4 = v1 + v2            v4 = t1 + v2
v5 = v3 + v4            v5 = v3 + v4
ret v5                  ret v5
2026 Fall
CSE411

Register Spilling Example

  • Suppose we spill v1.
                                          Live Variables
v1 = a + 1              v1 = a + 1        {a, b, v1}
                        store v1, [slot]  {a, b}
v2 = b + 2              v2 = b + 2        {a, b, v2}
v3 = a * b      ---->   v3 = a * b        {v2, v3}
                        t1 = load [slot]  {v2, v3, t1}
v4 = v1 + v2            v4 = t1 + v2      {v3, v4}
v5 = v3 + v4            v5 = v3 + v4      {v5}
ret v5                  ret v5            {}
2026 Fall
CSE411

Register Spilling

v = a + b      ---->   v = a + b
                       store v, [slot]

...

                       t1 = load [slot]
x = v + c      ---->   x = t1 + c
  • Insert a store instruction after each definition.
  • Insert a load instruction before each use.
  • Introduce a fresh virtual register for each reload.
2026 Fall
CSE411

Which Variable Should We Spill?

2026 Fall
CSE411

Which Variable Should We Spill?

  • f appears to be a good spill candidate given its highest degree (along with e).
2026 Fall
CSE411

Which Variable Should We Spill?

a = input();
b = input();
c = input();
d = input();
e = input();
f = input();

while (f < 1000000) {
    f = f + c;
}

x = a + b;
y = c + d;
z = e + f;

return x + y + z;
2026 Fall
CSE411

Which Variable Should We Spill?

  • Prioritize variables with high degrees in the interference graph.
  • Prioritize variables that are likely to be accessed infrequently at runtime.
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: empty
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b d
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b d c
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b d c f
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b d c
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b d
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a b
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: a
  • Spilled: e
2026 Fall
CSE411

Coloring by Simplification () + Register Spilling

  • Stack: empty
  • Spilled: e
2026 Fall
CSE411

Yet Another Example ()

  • Stack: empty
  • No node has a degree less than .
2026 Fall
CSE411

Yet Another Example ()

  • Stack: empty
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: 2
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: 2 3
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: 2 3 4
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: 2 3
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: 2
  • Spilled: 1
2026 Fall
CSE411

Yet Another Example ()

  • Stack: empty
  • Spilled: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: empty
  • No node has a degree less than .
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack:
  • Mark node 1 as a potential spill ().
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: 2
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: 2 3
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: 2 3 4
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: 2 3
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: 2
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack:
  • Potential spill: 1
2026 Fall
CSE411

Optimistic Coloring ()

  • Stack: empty
  • No actual spill is needed.
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: empty
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a
  • Mark e as a potential spill ().
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b d
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b d c
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b d c f
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b d c
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b d
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a b
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a
  • Potential spill: e
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: a
  • All three colors are used by neighbors of e, so e must be spilled.
2026 Fall
CSE411

Revisiting Earlier Example ()

  • Stack: empty
  • Spilled: e
2026 Fall
CSE411

Live but Idle Variables

                        Live Variables
v = input()             {v}
x = v + 1               {v}

a = input()             {v, a}
b = input()             {v, a, b}
c = input()             {v, a, b, c}
y = a + b               {v, c, y}
z = y + c               {v, z}

w = v + z               {w}
ret w                   {}
  • Assume we have three physical registers.
2026 Fall
CSE411

Spilling v

v = input()              v = input()
                         store v, [slot]
                         t1 = load [slot]
x = v + 1                x = t1 + 1         
a = input()              a = input()
b = input()     ------>  b = input()
c = input()              c = input()
y = a + b                y = a + b
z = y + c                z = y + c
                         t2 = load [slot]
w = v + z                w = t2 + z
ret w                    ret w
  • How many memory accesses are introduced?
    • 1 store, 2 loads
2026 Fall
CSE411

Live-Range Splitting

Original Code     Live Vars                                    Live Vars
v = input()       {v}                   v = input()            {v}
x = v + 1         {v}                   x = v + 1              {v}
                                        store v, [slot]        {}
a = input()       {v, a}                a = input()            {a}
b = input()       {v, a, b}             b = input()            {a, b}
c = input()       {v, a, b, c}  ---->   c = input()            {a, b, c}
y = a + b         {v, c, y}             y = a + b              {c, y}
z = y + c         {v, z}                z = y + c              {z}
                                        t = load [slot]        {z, t}
w = v + z         {w}                   w = t + z              {w}
ret w             {}                    ret w                  {}
  • Memory accesses introduced
    • 1 store and 1 load
2026 Fall

# Q2: How to assign the registers to the nodes in the interference graph? ![width:800px](img/register/visited-countries.png) ---