Presentation is loading. Please wait.

Presentation is loading. Please wait.

SSA.

Similar presentations


Presentation on theme: "SSA."— 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 a3= a = a = a1= = a+5 = a1+5 = a+5 a4= Ф(a1,a3) 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 Vi, 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 Vi (in the new program). Then V and Vi 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? = A[i] = R(A,i) = R(A8,i7)
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(A8,i7) A9 = W(A8,j6,V5) = R(A9,k4)

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 i2 = Ф(i1,i3) A1 = Ф(A0,A2) A2= W(A1,i2,i2) i3 = i2 +1 until i3>10 repeat i2 = Ф(i1,i3) A2= HW(i2,i2) i3 = i2 +1 until i3>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 Vi

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 ENTRY a a EXIT b c b c d d CFG DT EXIT

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) = DFlocal(X)∪ ∪Z∈Children(X)DFup(z) DFlocal(X) = {Y∈Succ(x)|idom(Y)≠X} DFup(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 ∈ DFlocal(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 ∈DFup(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 Vi on rename stack i = i + 1 Rename all the Ф operands through successor edges Recursively rename for all child nodes in the dominator tree Pop Vi in X from the rename stack

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

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 Vi 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,V1 and V2 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 B1 i  1 j  1 k 0 B2 if k<100 B3 B4 if j<20
while(k<100) { if(j<20) j=i; k=k+1; } else j=k; k=k+2; return j; B2 if k<100 B3 B4 if j<20 return j B5 B6 j  i k  k+1 j  k k  k+2 B7

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

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

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

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

36 SSA Example 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 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;

37 SSA Example: Constant Propagation
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 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

38 SSA Example: Dead Code Elimination
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  (j3,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  (1,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? B2 j2  (j4,1) k2  (k4,0) if k2<100 B3 B4 return j2 SSA conditional constant propagation finds the least fixed point for the program and allows further elimination of dead code. if j2<20 B5 B6 k3  k2+1 j5  k2 k5  k2+2 B7 j4  (1,j5) k4  (k3,k5)

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

42 SSA Example: Single Argument  Function Elimination
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  (j4,1) k2  (k4,0) if k2<100 j4  1 k4  k3 B2 B5 B7 B4 B4

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 B4

44 SSA Example: More Dead Code
k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 B2 B5 B4 k3  k2+1 return j2 j2  (1,1) k2  (k3,0) if k2<100 j4  1 k4  k3 B2 B5 B7 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."

Similar presentations


Ads by Google