Presentation is loading. Please wait.

Presentation is loading. Please wait.

SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Similar presentations


Presentation on theme: "SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example."— Presentation transcript:

1 SSA

2 Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example

3 SSA Introduction SSA is a intermediate representation. SSA was developed by Ron Cytron, Jeanne Ferrante, Barry Rosen, Mark Wegman, and Ken Zadeck, researchers at IBM in the 1980s. Benefits –constant propagation –sparse conditional constant propagation –dead code elimination –global value numbering –partial redundancy elimination –strength reduction –register allocation

4 SSA Introduction Static Single Assignment Form –Each variable has only one reaching definition. –When two definitions merge, a Ф function is introduced to with a new definition of the variable. –A Ф operand represents the reaching definition from the corresponding predecessor.

5 SSA Introduction a1=a1= = a 1 +5 a2=a2= = a 2 +5 a3=a3= a 4 = Ф (a 1,a 3 ) = a 4 +5 a =a = = a+5 a =a = a =a =

6 SSA Introduction SSA Condition –If two nonnull paths X → + Z and Y → + Z converge at a node, and nodes X and Y contain assignments to V (in the original program), then a trivial  -function V =  (V,…,V) has been inserted at Z (in the new program) –Each mention of V in the original program or in an inserted  - function has been replaced by a mention of a new variable V i, leaving the new program in SSA form –Along any control flow path, consider any use of a variable V (in the original program) and the corresponding use of V i (in the new program). Then V and V i has same value.

7 SSA Introduction Where to insert  -functions? –By condition 1, a node Z needs a  -function for V because Z is a convergence point for two nonnull paths X → + Z and Y → + Z that start at nodes X and Y already containing assignments to V.

8 SSA Introduction What about arrays? –Treating A[i] as a variable would be awkward, both because an assignment to A[i] may or may not change the value of A[j] and because the value of A[i] could be changed by assigning to i rather than to A[i]. –Treat entire array as a scalar variable. = A[i] A[j] = V = A[k] = R(A,i) A = W(A,j,V) = R(A,k) = R(A 8,i 7 ) A 9 = W(A 8,j 6,V 5 ) = R(A 9,k 4 )

9 SSA Introduction W operator may introduce unnecessary liveness for A. Introduce HW (HiddenW). repeat A[i] = i i = i +1 until i>10 repeat i 2 = Ф(i 1,i 3 ) A 1 = Ф(A 0,A 2 ) A 2 = W(A 1,i 2,i 2 ) i 3 = i 2 +1 until i 3 >10 repeat i 2 = Ф(i 1,i 3 ) A 2 = HW(i 2,i 2 ) i 3 = i 2 +1 until i 3 >10

10 Converting to SSA Big picture, translation to SSA form is done in 3 steps –The dominance frontier mapping is constructed form the control flow graph. –Using the dominance frontiers, the locations of the  -functions for each variable in the original program are determined. –The variables are renamed by replacing each mention of an original variable V by an appropriate mention of a new variable V i

11 CFG A control flow graph G = (V, E) Set V contains distinguished nodes ENTRY and EXIT –every node is reachable from ENTRY –EXIT is reachable from every node in G. –ENTRY has no predecessors –EXIT has no successors. Notation: predecessor, successor, path

12 Dominance Relation If X appears on every path from ENTRY to Y, then X dominates Y. Dominance relation is both reflexive and transitive. idom(Y): immediate dominator of Y Dominator Tree –ENTRY is the root –Any node Y other than ENTRY has idom(Y) as its parent –Notation: parent, child, ancestor, descendant

13 Dominator Tree Example ENTRY a b c d EXIT ENTRY a d EXIT b c CFG DT

14 Dominator Tree Example

15 Dominance Frontier Dominance Frontier DF(X) for node X –Set of nodes Y –X dominates a predecessor of Y –X does not strictly dominate Y Equation 1: DF(X) = {Y|(  P  ∈ Pred(Y))(X  P and X  Y )} Equation 2: DF(X) = DF local (X) ∪ ∪ Z ∈ Children(X) DF up (z) DF local (X) = {Y ∈ Succ(x)|idom(Y)≠X} DF up (X) = {Y ∈ DF(Z)|idom(Z) ≠X}

16 Dominance Frontier How to proof equation 1 and equation 2 are correct? –Easy to See that => is correct –Still have to show everything in DF(X) has been accounted for. –Suppose Y ∈ DF(X) and U  Y be the edge that X dominate U but doesn’t strictly dominate Y. –If U == X, then Y ∈ DF local (X) –If U ≠X, then there exists a path from X to U in Dominator Tree which implies there exists a child Z of X dominate U. Z doesn’t strictly dominate Y because X doesn’t strictly dominate Y. So Y ∈ DF up (X).

17 Ф-function and Dominance Frontier Intuition behind dominance frontier –Y ∈ DF(X) means: Y has multiple predecessors X dominate one of them, say U, U inherits everything defined in X Reaching definition of Y are from U and other predecessors So Y is exactly the place where Ф-function is needed

18 Control Dependences and Dominance Frontier A CFG node Y is control dependent on a CFG node X if both the following hold: –There is nonnull path p: X → + Y such that Y postdominate every node after X. –The node Y doesn’t strictly postdominate the node X If X appears on every path from Y to Exit, then X postdominate Y.

19 Control Dependences and Dominance Frontier In other words, there is some edge from X that definitely causes Y to execute, and there is also some path from X that avoids executing Y.

20 RCFG The reverse control flow graph RCFG has the same nodes as CFG, but has edge Y →X for each edge X→Y in CFG. Entry and Exit are also reversed. The postdominator relation in CFG is dominator relation in RCFG. Let X and Y be nodes in CFG. Then Y is control dependent on X in CFG iff X ∈ DF(Y) in RCFG.

21 SSA Construction– Place Ф Functions For each variable V –Add all nodes with assignments to V to worklist W –While X in W do For each Y in DF(X) do –If no Ф added in Y then »Place (V = Ф (V,…,V)) at Y »If Y has not been added before, add Y to W.

22 SSA Construction– Rename Variables Rename from the ENTRY node recursively For node X –For each assignment (V = …) in X Rename any use of V with the TOS of rename stack Push the new name V i on rename stack i = i + 1 –Rename all the Ф operands through successor edges –Recursively rename for all child nodes in the dominator tree –For each assignment (V = …) in X Pop V i in X from the rename stack

23 Rename Example a1=a1= a 1 +5 a = a+5 a= a= Ф (a 1,a) a+5 TOS Rename expr a1 a0

24 Converting out of SSA Eventually, a program must be executed. The Ф-function have precise semantics, but they are generally not represented in existing target machines.

25 Converting Out of SSA Naively, a k-input Ф-function at entrance to node X can be replaced by k ordinary assignments, one at the end of each control predecessor of X. Inefficient object code can be generated.

26 Dead Code Elimination Where does dead code come from? –Assignments without any use Dead code elimination method –Initially all statements are marked dead –Some statements need to be marked live because of certain conditions –Mark these statements can cause others to be marked live. –After worklist is empty, truly dead code can be removed

27 Allocation By Coloring It might seem possible to map all occurrence of V i back to V and delete all Ф-functions. After certain optimization, this assumption might not be true.

28 Allocation By Coloring Use graph coloring techniques to allocate storage for variables. In the case above,V 1 and V 2 are alive simultaneously, they have different color, and will be converted back to two different variables.

29 SSA Example Dead Code Elimination Intuition –Because there is only one definition for each variable, if the list of uses of the variable is empty, the definition is dead. –When a statement v  x  y is eliminated because v is dead, this statement must be removed from the list of uses of x and y. This might cause those definitions to become dead.

30 SSA Example Simple Constant Propagation Intuition –If there is a statement v  c, where c is a constant, then all uses of v can be replaced for c. –A  function of the form v   (c1, c2, …, cn) where all ci are identical can be replaced for v  c. –Using a work list algorithm in a program in SSA form,we can perform constant propagation in linear time.

31 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i  1 j  1 k  0 j  i k  k+1 j  k k  k+2 return j if j<20 if k<100 B1 B2 B3 B5 B6 B4 B7

32 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 if k<100 B1 B2 B3 B5 B6 B4 B7

33 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 if k<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

34 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i  1 j  1 k1  0 j  i k3  k+1 j  k k5  k+2 return j if j<20 k2   (k4,k1) if k<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

35 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i  1 j  1 k1  0 j  i k3  k2+1 j  k k5  k2+2 return j if j<20 k2   (k4,k1) if k2<100 k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

36 SSA Example i=1; j=1; k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } return j; } i1  1 j1  1 k1  0 j3  i1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,j1) k2   (k4,k1) if k2<100 j4   (j3,j5) k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

37 SSA Example: Constant Propagation i1  1 j1  1 k1  0 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (j3,j5) k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7 i1  1 j1  1 k1  0 j3  i1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,j1) k2   (k4,k1) if k2<100 j4   (j3,j5) k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7

38 SSA Example: Dead Code Elimination i1  1 j1  1 k1  0 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (j3,j5) k4   (k3,k5) B1 B2 B3 B5 B6 B4 B7 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (j3,j5) k4   (k3,k5) B2 B3 B5 B6 B4 B7

39 SSA Example: Constant Propagation and Dead Code Elimination j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1,j5) k4   (k3,k5) B2 B3 B5 B6 B4 B7 j3  1 k3  k2+1 j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (j3,j5) k4   (k3,k5) B2 B3 B5 B6 B4 B7

40 SSA Example: One Step Further But block 6 is never executed! How can we find this out, and simplify the program? SSA conditional constant propagation finds the least fixed point for the program and allows further elimination of dead code. k3  k2+1j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1,j5) k4   (k3,k5) B2 B3 B5 B6 B4 B7

41 SSA Example: Dead Code Elimination k3  k2+1j5  k2 k5  k2+2 return j2 if j2<20 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1,j5) k4   (k3,k5) B2 B3 B6 B4 B7 B4 k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1) k4   (k3) B2 B5 B7

42 SSA Example: Single Argument  Function Elimination B4 k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4   (1) k4   (k3) B2 B5 B7

43 SSA Example: Constant and Copy Propagation k3  k2+1 return j2 j2   (j4,1) k2   (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4

44 SSA Example: More Dead Code k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 B2 B5 B4

45 SSA Example: More  Function Simplification k3  k2+1 return j2 j2   (1,1) k2   (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return j2 j2  1 k2   (k3,0) if k2<100 B2 B5 B4

46 SSA Example: More Constant Propagation k3  k2+1 return j2 j2  1 k2   (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return 1 j2  1 k2   (k3,0) if k2<100 B2 B5 B4

47 SSA Example: Ultimate Dead Code Elimination k3  k2+1 return 1 j2  1 k2   (k3,0) if k2<100 B2 B5 B4 return 1 B4


Download ppt "SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example."

Similar presentations


Ads by Google