CSE411

CSE411: Introduction to Compilers

Register Allocation

Jooyong Yi (UNIST)

CSE411

Register Allocation Problem

  • The problem at hand: IR 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 address.
    • But this is too slow.
  • Goal: rewrite the IR to maximally utilize the physical registers, while minimizing the use of memory.
CSE411

Register Allocation Problem Example

v3 = v1 + v2
v4 = v1 + v3
ret v4
  • Assume we have only two physical registers, r1 and r2.
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
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
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)
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
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
CSE411

Interference Graph

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

Solving Register Allocation Problem with Interference Graph

    v1:r1  ------ v2: ?
    | 
    |
    v3            v4
CSE411

Solving Register Allocation Problem with Interference Graph

    v1:r1  ------ v2: r2
    | 
    |
    v3            v4
CSE411

Solving Register Allocation Problem with Interference Graph

    v1:r1  ------ v2: r2
    | 
    |
    v3:r2         v4: r1 or r2
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
CSE411

General Strategy

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

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

CSE411

Graph Coloring Problem

  • Adjacent nodes must have different colors.
CSE411

Graph Coloring Problem

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

Idea

  • Assume that we have 3 colors.
CSE411

Idea

  • Assume that we have 3 colors.
  • If we can successfully color , we can color .
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 .
CSE411

Simplification Example ()

CSE411

Simplification Example ()

  • Stack: 5
CSE411

Simplification Example ()

  • Stack: 5 2
CSE411

Simplification Example ()

  • Stack: 5 2 1
CSE411

Simplification Example ()

  • Stack: 5 2 1 3
CSE411

Simplification Example ()

  • Stack: 5 2 1 3
CSE411

Simplification Example ()

  • Stack: 5 2 1
CSE411

Simplification Example ()

  • Stack: 5 2
CSE411

Simplification Example ()

  • Stack: 5
CSE411

Simplification Example ()

  • Stack:
CSE411

Another Example ()

CSE411

Another Example ()

CSE411

Another Example ()

  • Pick a node and remove it from the graph.
CSE411

Another Example ()

  • Continue the simplification process.
  • Hope: {b, c, d, e} use less than 3 colors.
    • This idea is called optimistic coloring.
CSE411

Another Example ()

  • Continue the simplification process.
  • Hope: {b, c, d, e} use less than 3 colors.
    • This idea is called optimistic coloring.
  • Does not work in our example and we need to spill f into the memory.
CSE411

Spilling

  • Before each operation that reads a spilled variable , insert
    • = load // : the memory address where the value of is stored
  • After each operation that writes to a spilled variable , insert
    • store , // store the value of to
CSE411

Spilling

  • Which one to spill?
    • a node that interferes with many other nodes.
    • a node that is not frequently used.
CSE411

Register Allocation

  1. Build the interference graph
  2. Apply the simplification algorithm
    1. If stuck, remove a node and try with the optimistic coloring.
    2. If the optimistic coloring fails, spill the node to the memory.
      1. This involves rewriting the code to load and store the value of the spilled variable.
      2. Repeat step 1.

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