lecture-7
lecture-7
7 – Register Allocation
Hugh Leather
IF 1.18a
[email protected]
2019
Introduction
This lecture:
Local Allocation - spill code
Global Allocation based on graph colouring
Techniques to reduce spill code
Register allocation
Requirements
Produce correct code that uses k (or fewer) registers
Minimise added loads and stores
Minimise space used to hold spilled values
Operate efficiently
O(n), O(nlog2 n), maybe O(n2 ), but not O(2n )
Register allocation
Definitions
Interference
Two valuesa cannot be mapped to the same register wherever they
are both liveb
Such values are said to interfere
a
A value is stored in a variable
b
A value is live from its definition to its last use
Live range
The live range of a value is the set of statements at which it is live
May be conservatively overestimated (e.g. just begin → end)
Register allocation
Definitions
Spilling
Spilling saves a value from a register to memory
That register is then free – Another value often loaded
Requires F registers to be reserved
Spilling in ILOC
F is 0 (assuming rarp already reserved)
Dirty value Clean value
storeAI rx → rarp , @x loadAI rarp , @y ⇒ ry
loadAI rarp , @y ⇒ ry
Local register allocation
Algorithm:
If number of values > k
Rank values by occurrences
Allocate first k - F values to registers
Spill other values
Local register allocation
Top down
Algorithm:
Start with empty register set
Load on demand
When no register is available, free one
Replacement:
Spill the value whose next use is farthest in the future
Prefer clean value to dirty value
Local register allocation
Top down
1
Local allocation is NP-complete with dirty vs clean
Global register allocation
Algorithm sketch
Definition
A graph G is said to be k-colourable iff the nodes can be labeled
with integers 1 ... k so that no edge in G connects two nodes with
the same label
Examples
Global register allocation
Interference graph
2
Two values interfere wherever they are both live
Two live ranges interfere if their values interfere at any point
Global register allocation
Colouring the interference graph
3
Degree is number of neighbours
4
Proof as exercise
Global register allocation
Chaitin’s algorithm
k=2
Graph is 2-colourable
Chaitin must immediately spill one of these nodes
Briggs said, take that same node and push it on the stack
When you pop it off, a colour might be available for it!
Chaitin-Briggs algorithm uses this to colour that graph
Global register allocation
Chaitin-Briggs algorithm
Splitting example
Non contiguous live ranges - cannot be 2 coloured
Global register allocation
Live ranges splitting
Splitting example
Split live ranges - can be 2 coloured
Global register allocation
Coalescing