Title: Register Allocation 1 of 2
1Register Allocation 1 of 2
2Live Variables Analysis
Definition a variable x is live after a
statement S1 iff there is a path through CFG from
S1 to another statement S2 which uses x and x is
not defined on that path.
1
1
z0
z0
2
2
y gt 1
y gt 1
yes
no
yes
no
3
4
3
4
zyy
zyy
zyy
wyy
5
5
yz1
yz1
(z not live after 1)
(z is live after 1)
3Live Variables Analysis
Definition a variable x is live after a
statement S1 iff there is a path through CFG from
S1 to another statement S2 which uses x and x is
not defined on that path.
1
1
z0
z0
2
2
y gt 1
y gt 1
yes
no
yes
no
3
4
3
4
zyy
zyy
zyy
wyy
5
5
yz1
yz1
(z not live after 1)
(z is live after 1)
4Live Variables Analysis
Definition a live range for a variable x is a
set of CFG edges, where x is live on each edge.
0
y1
1
z0
Live range for y
2
y gt 1
yes
no
3
4
zyy
zyy
Live range for z
5
yz1
5Register Allocation
- Objective minimise number of registers required
- Simplistic approach allocate each local variable
to its own register - But, we can do much better!
- The following CFG requires one register, as live
ranges for y and z do not overlap
0
y1
1
z0
Live range for y
2
y gt 1
yes
no
3
4
zyy
zyy
Live range for z
5
yz1
6Register Allocation
- Another Example
- For this CFG, two registers needed because live
ranges for y and z overlap
0
y1
1
z0
2
y gt 1
Live range for y
yes
no
3
4
zyy
zyy
Live range for z
5
yzy
7Register Allocation
- Interference Graph
- An undirected graph
- Two nodes connected by edge if their live ranges
overlap
0
y1
1
z0
y
2
y gt 1
Live range for y
yes
no
z
3
4
zyy
zyy
Live range for z
5
yzy
Interference Graph
Control-flow Graph
Live Ranges
8Graph Colouring
Definition an undirected graph is said to be
coloured iff every vertex can be given a colour
where, for each edge x-y, the colours of x and y
are different.
Definition the chromatic number of a graph is
the minimal number of colours required to colour
the graph.
- Graph Colouring is an NP-Complete problem!
- How many colours required for these graphs?
y
y
x
y
x
t
z
z
w
w
z
9Graph Colouring
Definition an undirected graph is said to be
coloured iff every vertex can be given a colour
where, for each edge x-y, the colours of x and y
are different.
Definition the chromatic number of a graph is
the minimal number of colours required to colour
the graph.
- Graph Colouring is an NP-Complete problem!
- How many colours required for these graphs?
y
y
x
y
x
y
z
z
w
w
z
10Register Allocation Graph Colouring
- Interference Graph
- Chromatic number gives minimal number of
registers required!
c3
a
dab
b
c
edc
return ec
d
e
Interference Graph
11Register Allocation Graph Colouring
- Interference Graph
- Chromatic number gives minimal number of
registers required!
c3
a
dab
b
c
edc
return ec
d
e
Interference Graph
12Register Allocation Instruction Ordering
- Instruction ordering affects chromatic number!
dab
a
c3
b
c
edc
return ec
d
e
Interference Graph
13Computing Live Variables
- Objective compute in and out sets for each
statement - Livein(n) is set of live variables just before
statement n - Liveout(n) is set of live variables just after
statement n
0
y1
Well omit this node for simplicity
Livein(1) y , Liveout(1) y
1
z0
Live range for y
Livein(2) y , Liveout(2) y
2
y gt 1
yes
no
Livein(3) y , Liveout(3) z
3
4
zyy
zyy
Livein(4) y , Liveout(4) z
Live range for z
5
Livein(5) z , Liveout(5)
zz1
14Computing Live Variables
Definition a variable x is defined by a
statement S1 if it is assigned in S1.
Definition a variable x is used by a statement
S1 if it is read in S1.
def(1) z , use(1)
def(2) , use(2) y
def(3) z , use(3) y
def(4) z , use(4) y
def(5) z , use(5) z
15An inefficient algorithm!
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
16An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1
17An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1
18An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1
19An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1
20An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1
21An inefficient algorithm!
y z
for(Stmt s statements) Liveout(s) ø
for(Var x variables) for(Path p
pathsFrom(s)) for(Node n p) if(x ?
use(n)) Liveout(s) ? x break to
next Var if(x ? def(n))
break
1
z0
2
y gt 1
yes
no
3
4
zyy
zyy
5
zz1