Translating Out of Static Single Assignment Form

advertisement
Translating Out of Static Single
Assignment Form
Authors: Vugranam C. Sreedhar, Roy Dz-Ching Ju,
David M. Gilles and Vatsa Santhanam
Reader: Pushpinder Kaur Chouhan
1
Translating Out of SSA Form
 Introduction
 Basic Keywords
 Algorithm for going out of SSA form
 Experimental Results
 Conclusion
 References
2
Translating Out of SSA Form

Introduction









Definition of SSA form
Role of SSA form in Compiler
A new framework for leaving SSA form
A new CSSA-based coalescing algorithm
Basic Keywords
Algorithm for going out of SSA form
Experimental Results
Conclusion
References
3
Introduction
 Static Single Assignment form is an intermediate representation that
compilers use to facilitate program analysis and optimization.

Original
Intermediate Code
P0
P1
Optimized
Intermediate Code
P2
……………………………………
Pn
Role of SSA form in a compiler
 Algorithm for going out of SSA form.
 Algorithm for eliminating redundant copies.
4
Translating Out of SSA Form
 Introduction
 Basic Keywords
 Phi Congruence Class
 Phi Congruence Property
 Liveness and Interference




Algorithm for going out of SSA form
Experimental Results
Conclusion
References
5
Basic Keywords
 Phi Congruence Class[x] is
the reflexive and
transitive closure of phiConnectedResource(x), where
phiConnectedResource(x) = {y | x and y are referenced in
the same phi instruction} .
 Phi Congruence Property states that the
occurrences of all resources which belong to the same phi
congruence class in a program can be replaced by
representative resource.
6
Basic Keywords
 Liveness and Interference –
A variable is live at a program point
P if there exists a path from P to a use of x that contains
no definition of x.
Two variables in a program are said
to interferer if their live ranges overlap at any program
point.
 LiveIn[L]: The set of resources that are live at the
beginning of the basic block L.
 LiveOut[L]: The set of resources that are live at the end
of the basic block L.
7
Translating Out of SSA Form
 Introduction
 Basic Keywords
 Algorithm for going out of SSA form
 Translating the TSSA form to a CSSA form
 Eliminating redundant copies
 Eliminating phi instructions and leaving the CSSA form
 Experimental Results
 Conclusion
 References
8
Algorithm for going out of SSA form

Translating the TSSA form to a CSSA form
 Naïve Translation
 Translation based on Interference Graph
 Translation based on Data Flow and Interference Graph

Eliminating redundant copies
 CSSA-based coalescing algorithm

Eliminating phi instruction and leaving the CSSA form
 Eliminate the phi instruction by replacing all references in the phi
instruction by a representative resource.
9
Translating the TSSA form to a CSSA form
TSSA form – The SSA form transformed to a state in which there are
phi resource interference.
CSSA form – The SSA from that has the phi congruence property.
L1
Y=
X1=
L1
X2=y
x3=phi(x1:L1, x2:L2)
Z=x3
x2=
X1=
L2
L2
L3
An example program in SSA form
x3=phi(x1:L1, x2:L2)
Z=x3
L3
An example program in TSSA form
10
Translating the TSSA form to a CSSA form
 Naïve Translation method
 Insert the copies for all resources referenced in a phi instruction.
L1
x2=
x1=
L1
x2=
x3=phi(x1:L1, x2:L2)
Z=x3
x2=
X1=
x1’=x1
x2’=x2
L2
L3
An example program in TSSA form
x3’=phi(x1’:L1, x2’:L2)
x3=x3’
Z=x3
CSSA form
L2
L3
11
Translating the TSSA form to a CSSA form
 Translation based on Interference Graph
 Insert the copies only if resources of phi instruction interfere.
L1
x2=
x1=
L1
x2=
x3=phi(x1:L1, x2:L2)
Z=x3
x2=
X1=
x1’=x1
x2’=x2
L2
L3
An example program in TSSA form
x3=phi(x1’:L1, x2’:L2)
Z=x3
CSSA form
L2
L3
12
Translating the TSSA form to a CSSA form
 Translation based on Data Flow and Interference Graph
 Use LiveOut sets to eliminate the interference among phi source resources.
 Use LiveIn and LiveOut sets to eliminate interferences between the target
resource and a source resource.
L1
x2=
x1=
L1
x2=
x3=phi(x1:L1, x2:L2)
Z=x3
x2=
X1=
x2’=x2
L2
L3
An example program in TSSA form
x3=phi(x1:L1, x2’:L2)
Z=x3
CSSA form
L2
L3
13
Main Feature of Algorithm
First check whether for any pair of resources, xi:Li and xj:Lj in
a phi instruction, where 0<=i, j<=n and xi =! xj, there exists
resource yi in phiCongruenceClass[xi], yj in
phiCongruenceClass[xj] and yi and yj interfere.
If so we will insert copies to ensure that xi and xj will not be
put in the same phi congruence class. Consider the case in
which both xi and xj are source resources in the phi
instruction.
There are four cases to consider to insert copies instructions
for resources in the phi instruction.
14
CASE 1. The intersection of phiCongruenceClass[xi] and LiveOut[Lj] is
not empty and the intersection of phiCongruenceClass[xj] and LiveOut[Li]
is empty. A new copy, xi’=xi, is needed in Li.
CASE 2. The intersection of phiCongruenceClass[xi] and LiveOut[Lj] is
empty and the intersection of phiCongruenceClass[xj] and LiveOut[Li] is
not empty. A new copy, xj’=xj, is needed in Lj.
CASE 3. The intersection of phiCongruenceClass[xi] and LiveOut[Lj] is
not empty, and the intersection of phiCongruenceClass[xj] and LiveOut[Li]
is not empty. Two new copies, xi’=xi in Li and xj’=xj in Lj, are needed to
ensure that xi and xj are put in different phi congruence classes.
CASE 4. The intersection of phiCongruenceClass[xi] and LiveOut[Lj] is
empty, and the intersection of phiCongruenceClass[xj] and LiveOut[Li] is
empty. Either a copy, xi’=xi in Li, or a copy, xj’=xj in Lj, is sufficient to
eliminate the interference between xi and xj. However, the final decision of
which copy to insert is deferred until all pairs of interfering resources in the
phi instruction are processed.
15
An example to illustrate the algorithm of Method III
X3=
X1=
L3
phiCongruenceClass[xi]={xi}
LiveOut sets:
L1={x1}
L4
L2={x2}
L3={x1,x3}
L1
L0
X1=
L2
X2=
X0=phi(X1:L1,X2:L2, X3:L3)
Use LiveOut sets to eliminate the interference among phi source resources.
16
An example to illustrate the algorithm of Method III
phiCongruenceClass[x1]={x1}
X3=
X1=
L3
phiCongruenceClass[x2]={x2}
LiveOut[1]={x1}
L4
LiveOut[2]={x2}
According to Case 4.
L1
L0
X1=
L2
X2=
X2’=x2
X0=phi(X1:L1,X2:L2, X3:L3)
Use LiveOut sets to eliminate the interference among phi source resources.
17
An example to illustrate the algorithm of Method III
phiCongruenceClass[x1]={x1}
X3=
X1=
L3
phiCongruenceClass[x3]={x3}
LiveOut[1] = {x1}
L4
LiveOut[3] = {x1,x3}
According to Case 1.
L1
L0
X1=
X1’=x1
L2
X2=
X2’=x2
X0=phi(X1:L1,X2:L2, X3:L3)
Use LiveOut sets to eliminate the interference among phi source resources.
18
An example to illustrate the algorithm of Method III
L1
x1=
LiveOut[L2] = {x2,x3}
L2
x2=phi(x1:L1,x3:L2)
x3=x2+1
L3
X2 and x3 interfere
=x2
LiveIn[L2] = {x2}
L1
x1=
L2
x2’=phi(x1:L1,x3:L2)
x2=x2’
x3=x2+1
=x2
L3
Use LiveIn and LiveOut sets to eliminate interferences
between the target resource and a source resource.
19
Eliminating Redundant Copies
For eliminating redundant copies, CSSA-based coalescing algorithm can
be used, as it can eliminate copies even when their live ranges interfere, so
long as the coalesced live range does not introduced any phi resource
interference.
CASE 1: phiCongruenceClass[x]=={} and phiCongruenceClass[y]=={}.
This means that x and y are not referenced in any phi instruction. The
copy can be removed even if x and y interfere.
CASE 2: phiCongruenceClass[x]=={} and phiCongruenceClass[y]
=! {}. If x interferes with any resource in (phiCongruenceClass[y]-y)
then the copy can not be removed, otherwise it can be removed.
CASE 3: phiCongruenceClass[x] =! {}and phiCongruenceClass[y]==
{}. If y interferes with any resource in (phiCongruenceClass[x]- x) then
the copy can not be removed, otherwise it can be removed.
CASE 4: phiCongruenceClass[x] =! {} and phiCongruenceClass[y]
=! {}. The copy cannot be removed if any resource in
phiCongruenceClass[x] interferes with any resource in
(phiCongruenceClass[y]-y) or if any resource in phiCongruenceClass[
y] interferes with any resource in (phiCongruenceClass[x]x), otherwise it can be removed.
20
Example of Copy Elimination
Y1=30
X1=y1
Y2=10
Y3=phi(y1,y2)
Foo(y3)
X2=20
x3=phi(x1,x2)
Goo(x3)
Y=30
Foo(y3)
Y=10
X=20
Goo(x3)
PhiCongruenceClass[x1] = {x1,x2,x3}
PhiCongruenceClass[y1] = {y1,y2,y3}
21
Eliminating phi instructions and leaving the CSSA form
Eliminate the phi instruction by replacing all references in the phi instruction
(belonging to the same phi congruence class) by a representative resource.
L1
x1=
L1
x=
L2
L2
x2’=phi(x1:L1,x3:L2)
x2=x2’
x3=x2+1
x2=x
x=x2+1
=x2
=x2
L3
L3
Replace x1,x2’ and x3 by x
22
Translating Out of SSA Form
 Introduction
 Basic Keywords
 Algorithm for going out of SSA form
 Experimental Results
 Based on compilation time
 Based on space usage
 Conclusion
 References
23
Experimental Results
Procedure
Name
BT
Yylex
(gcc)
1573
I
II
III
4632
1825
1648
3050
252
74
Ttin
(o/s code)
539
I
II
III
2389
1369
761
1850
830
222
%
Improvem
ent
Meth
ods
I
II
III
AT
*
*
*
AT-BT
*
72.1
89.9
AC
DF/IG
TT
TC
Total
(secs)
(secs)
(secs)
(secs)
660
670
493
6.37
5.30
5.86
0.60
0.90
0.38
3.78
1.61
1.62
10.75
7.81
7.86
826
1201
600
3.44
2.43
2.59
0.20
2.21
0.87
1.41
0.30
0.18
5.05
4.94
3.64
*
-29.1
8.6
*
*
*
*
*
*
*
*
*
*
13.1
15.1
24
Experimental Results
 Based on space usage –
 Method III introduce 90% fewer copies than method I
 Method II introduce 72% fewer copies than method I
 Based on Compilation time  Method III is 15% better than method I
 CSSA-based coalescing algorithm –
 Most effective for method I
25
Conclusion
 LiveOut sets are used to eliminate interference among
phi source resources.
 LiveOut and LiveIn sets are used to eliminate
interference between the target resource and a source
resource.

Translation based on Data Flow and Interference Graph
is best among three methods for translating TSSA form to
CSSA form.
 CSSA-based coalescing algorithm can eliminate copies
even when the source resource and destination resource
interfere, if certain constraints are satisfied.
26
References
 P. Briggs, K. Cooper, T. Harvey and Taylor Simpson.
”Practical Improvements to the Construction and
Destruction of Static Single Assignment Form”.
 R. Cytron, J.Ferrente, B.K. Rosen, M.N. Wegman and
F.K. Zadeck. ”Esiently Computing Static Single
Assignment Form and the Control Dependence Graph”.
 Andrew W. Appel – “Modern Compiler Implementation in
ML”
27
Questions ?
28
What is the need of translation?


To eliminate the phi instruction.
Phi instructions are only conceptual tool
29
Why is it useful




more compact representation than def-use and use-def
chains.
for most programs reduces space/time requirement
make data-flow analysis easy
representation explicitly converts definitions to their
uses and vise versa.

merging of value is explicit
30
Algorithm for method III
31
vise versa.
32
DU-Chains, UD-Chains
 A definition-use chain or DU-chain for a
definition D of variable v connects the D to
all uses of v that it can reach.
 A use-definition chain or UD-chain for a
use U of variable v connects U to all
definitions of v that reach it.
33
Data-dependence Graph
A data-dependence
graph has one node
for every variable
(basic block) and one
edge representing the
flow of data between
X=1
B2 Z > 2
the two nodes
Different types of data
dependence
B3 Y = X + 1
– Flow: def to use
– Anti: use to def
– Out: def to def
entry
Z > 1 B1
X = 2 B4
Z=X–3
B5 X = 4
B6 Z = X + 7
exit
34
Control Dependency
Definition - Let G be a CFG,
with X and Y nodes in G. Y is
control-dependent on X iff
1.There exists a directed path
T
P from X to Y with any Z in P
(excluding X and Y)
X=1
postdominated by Y and
B2 Z > 2
2. X is not postdominated by
T
Y (there are two edges out of B3 Y = X + 1
Y; traversing one edges
always leads to X, the other
may not lead to X)
entry
Z > 1 B1
F
X = 2 B4
F
Z=X–3
B5 X = 4
B6 Z = X + 7
exit
35
Download