350 likes | 500 Views
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. Translating Out of SSA Form. Introduction Basic Keywords Algorithm for going out of SSA form
E N D
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
Translating Out of SSA Form • Introduction • Basic Keywords • Algorithm for going out of SSA form • Experimental Results • Conclusion • References
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
Introduction • Static Single Assignment form is an intermediate representation that compilers use to facilitate program analysis and optimization. • Original Optimized Intermediate Code Intermediate Code P0 P1 P2 …………………………………… Pn Role of SSA form in a compiler • Algorithm for going out of SSA form. • Algorithm for eliminating redundant copies.
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
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 Propertystates that the occurrences of all resources which belong to the same phi congruence class in a program can be replaced by representative resource.
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 interfererif 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.
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
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.
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. x2= X1= Y= X1= L1 L1 L2 X2=y L2 x3=phi(x1:L1, x2:L2) Z=x3 x3=phi(x1:L1, x2:L2) Z=x3 L3 L3 An example program in TSSA form An example program in SSA form
Translating the TSSA form to a CSSA form • Naïve Translation method • Insert the copies for all resources referenced in a phi instruction. x2= X1= x1’=x1 x2= x1= L1 L1 x2’=x2 L2 x2= L2 x3’=phi(x1’:L1, x2’:L2) x3=x3’ Z=x3 x3=phi(x1:L1, x2:L2) Z=x3 L3 L3 An example program in TSSA form CSSA form
Translating the TSSA form to a CSSA form • Translation based on Interference Graph • Insert the copies only if resources of phi instruction interfere. x2= X1= x1’=x1 x2= x1= L1 L1 x2’=x2 L2 x2= L2 x3=phi(x1’:L1, x2’:L2) Z=x3 x3=phi(x1:L1, x2:L2) Z=x3 L3 L3 An example program in TSSA form CSSA form
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. x2= X1= x2= x1= L1 L1 x2’=x2 L2 x2= L2 x3=phi(x1:L1, x2’:L2) Z=x3 x3=phi(x1:L1, x2:L2) Z=x3 L3 L3 An example program in TSSA form CSSA form
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.
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.
An example to illustrate the algorithm of Method III X3= X1= L3 phiCongruenceClass[xi]={xi} LiveOut sets: L1={x1} L2={x2} L3={x1,x3} L4 L1 X1= L2 X2= X0=phi(X1:L1,X2:L2, X3:L3) L0 Use LiveOut sets to eliminate the interference among phi source resources.
An example to illustrate the algorithm of Method III phiCongruenceClass[x1]={x1} phiCongruenceClass[x2]={x2} LiveOut[1]={x1} LiveOut[2]={x2} According to Case 4. X3= X1= L3 L4 X2= X2’=x2 L1 X1= L2 X0=phi(X1:L1,X2:L2, X3:L3) L0 Use LiveOut sets to eliminate the interference among phi source resources.
An example to illustrate the algorithm of Method III phiCongruenceClass[x1]={x1} phiCongruenceClass[x3]={x3} LiveOut[1] = {x1} LiveOut[3] = {x1,x3} According to Case 1. X3= X1= L3 L4 X1= X1’=x1 X2= X2’=x2 L1 L2 X0=phi(X1:L1,X2:L2, X3:L3) L0 Use LiveOut sets to eliminate the interference among phi source resources.
An example to illustrate the algorithm of Method III L1 x1= X2 and x3 interfere LiveOut[L2] = {x2,x3} LiveIn[L2] = {x2} x1= L1 L2 L2 x2=phi(x1:L1,x3:L2) x3=x2+1 x2’=phi(x1:L1,x3:L2) x2=x2’ x3=x2+1 L3 L3 =x2 =x2 Use LiveIn and LiveOut sets to eliminate interferences between the target resource and a source resource.
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.
Example of Copy Elimination Y1=30 X1=y1 Y2=10 X2=20 Y=30 Y=10 X=20 Y3=phi(y1,y2) Foo(y3) x3=phi(x1,x2) Goo(x3) Foo(y3) Goo(x3) PhiCongruenceClass[x1] = {x1,x2,x3} PhiCongruenceClass[y1] = {y1,y2,y3}
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. x1= x= L1 L1 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
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
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
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.
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”
What is the need of translation? • To eliminate the phi instruction. • Phi instructions are only conceptual tool
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
vise versa. vise versa. vise versa.
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.
entry B1 Z > 1 B4 X = 1 Z > 2 X = 2 B2 Z = X – 3 X = 4 B3 B5 Y = X + 1 B6 Z = X + 7 exit Data-dependence Graph A data-dependence graph has one node for every variable (basic block) and one edge representing the flow of data between the two nodes Different types of data dependence • Flow: def to use • Anti: use to def • Out: def to def
entry B1 Z > 1 B4 X = 1 Z > 2 X = 2 B2 Z = X – 3 X = 4 B3 B5 Y = X + 1 B6 Z = X + 7 exit Control Dependency Definition - Let G be a CFG, with X and Y nodes in G. Y is control-dependent on X iff • There exists a directed path P from X to Y with any Z in P (excluding X and Y) postdominated by Y and 2. X is not postdominated by Y (there are two edges out of Y; traversing one edges always leads to X, the other may not lead to X) T F F T