Register Allocation 1 of 2 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Register Allocation 1 of 2

Description:

Definition: the chromatic number of a graph is the minimal number of colours ... Chromatic number gives minimal number of registers required! d=a b. e=d c. return e c ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 22
Provided by: mcsV
Category:

less

Transcript and Presenter's Notes

Title: Register Allocation 1 of 2


1
Register Allocation 1 of 2
  • COMP431 (Compilers)

2
Live 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)
3
Live 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)
4
Live 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
5
Register 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
6
Register 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
7
Register 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
8
Graph 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
9
Graph 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
10
Register 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
11
Register 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
12
Register Allocation Instruction Ordering
  • Instruction ordering affects chromatic number!

dab
a
c3
b
c
edc
return ec
d
e
Interference Graph
13
Computing 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
14
Computing 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
15
An 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
16
An 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
17
An 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
18
An 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
19
An 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
20
An 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
21
An 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
Write a Comment
User Comments (0)
About PowerShow.com