Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stanford University CS243 Winter 2006 Wei Li 1 SSA.

Similar presentations


Presentation on theme: "Stanford University CS243 Winter 2006 Wei Li 1 SSA."— Presentation transcript:

1 Stanford University CS243 Winter 2006 Wei Li 1 SSA

2 CS243 Winter 2006 Stanford University 2 Overview SSA Representation SSA Representation SSA Construction SSA Construction Converting out of SSA Converting out of SSA

3 CS243 Winter 2006 Stanford University 3 Static Single Assignment Each variable has only one reaching definition. Each variable has only one reaching definition. When two definitions merge, a Ф function is introduced to with a new definition of the variable. When two definitions merge, a Ф function is introduced to with a new definition of the variable. First consider SSA for alias free variables. First consider SSA for alias free variables.

4 CS243 Winter 2006 Stanford University 4 Example: CFG a =a = = a+5 a =a = a =a = Multiple reaching definitions

5 CS243 Winter 2006 Stanford University 5 Example: SSA Form a1=a1= = a 1 +5 a2=a2= = a 2 +5 a3=a3= a 4 = Ф (a 1,a 3 ) = a 4 +5 Single reaching definition

6 CS243 Winter 2006 Stanford University 6 Ф Functions A Ф operand represents the reaching definition from the corresponding predecessor. A Ф operand represents the reaching definition from the corresponding predecessor. The ordering of Ф operands are important for knowing from which path the definition is coming from. The ordering of Ф operands are important for knowing from which path the definition is coming from.

7 CS243 Winter 2006 Stanford University 7 SSA Conditions 1. If two nonnull paths X → + Z and Y → + Z converge at node Z, and nodes X and Y contains (V =..), then V = Ф(V,.., V) has been inserted at Z. 2. Each mention of V has been replaced by a mention of V i 3. V and the corresponding V i have the same value.

8 CS243 Winter 2006 Stanford University 8 Overview SSA Representation SSA Representation SSA Construction SSA Construction Step 1: Place Ф statements Step 1: Place Ф statements Step 2: Rename all variables Step 2: Rename all variables Converting out of SSA Converting out of SSA

9 CS243 Winter 2006 Stanford University 9 Ф Placement a = … a = Ф (a,a) Ф Place minimal number of Ф functions

10 CS243 Winter 2006 Stanford University 10 Renaming a1=a1= a 1 +5 a 4 = a 4 +5 a2=a2= a 3 = Ф (a 1,a 2 ) a 3 +5

11 CS243 Winter 2006 Stanford University 11 SSA Construction (I) Step 1: Place Ф statements by computing iterated dominance frontier Step 1: Place Ф statements by computing iterated dominance frontier

12 CS243 Winter 2006 Stanford University 12 CFG A control flow graph G = (V, E) A control flow graph G = (V, E) Set V contains distinguished nodes START and END Set V contains distinguished nodes START and END every node is reachable from START every node is reachable from START END is reachable from every node in G. END is reachable from every node in G. START has no predecessors START has no predecessors END has no successors. END has no successors. Predecessor, successor, path Predecessor, successor, path

13 CS243 Winter 2006 Stanford University 13 Dominator Relation If X appears on every path from START to Y, then X dominates Y. If X appears on every path from START to Y, then X dominates Y. Domination is both reflexive and transitive. Domination is both reflexive and transitive. idom(Y): immediate dominator of Y idom(Y): immediate dominator of Y Dominator Tree Dominator Tree START is the root START is the root Any node Y other than START has idom(Y) as its parent Any node Y other than START has idom(Y) as its parent Parent, child, ancestor, descendant Parent, child, ancestor, descendant

14 CS243 Winter 2006 Stanford University 14 Dominator Tree Example START a b c d END START CFG DT

15 CS243 Winter 2006 Stanford University 15 Dominator Tree Example START a b c d END START a CFG DT

16 CS243 Winter 2006 Stanford University 16 Dominator Tree Example START a b c d END START a b c CFG DT

17 CS243 Winter 2006 Stanford University 17 Dominator Tree Example START a b c d END START a db c CFG DT

18 CS243 Winter 2006 Stanford University 18 Dominator Tree Example START a b c d END START a d END b c CFG DT

19 CS243 Winter 2006 Stanford University 19 Dominance Frontier Dominance Frontier DF(X) for node X Dominance Frontier DF(X) for node X Set of nodes Y Set of nodes Y X dominates a predecessor of Y X dominates a predecessor of Y X does not strictly dominate Y X does not strictly dominate Y

20 CS243 Winter 2006 Stanford University 20 DF Example START a b c d END START a d END b c CFG DT DF(c) = ? DF(a) = ?

21 CS243 Winter 2006 Stanford University 21 DF Example START a b c d END START a d END b c CFG DT DF(c) = {d} DF(a) = ?

22 CS243 Winter 2006 Stanford University 22 DF Example START a b c d END START a d END b c CFG DT DF(c) = {d} DF(a) = {END}

23 CS243 Winter 2006 Stanford University 23 Computing DF DF(X) is the union of the following sets DF(X) is the union of the following sets DF local (X), a set of successor nodes that X doesn’t strictly dominate DF local (X), a set of successor nodes that X doesn’t strictly dominate E.g. DF local (c) = {d} E.g. DF local (c) = {d} DF up (Z) for all Z є Children(X) DF up (Z) for all Z є Children(X) DF up (Z) = {Y є DF(Z) | idom(Z) doesn’t strictly dominate Y} DF up (Z) = {Y є DF(Z) | idom(Z) doesn’t strictly dominate Y} E.g. X = a, Z = d, Y = END E.g. X = a, Z = d, Y = END

24 CS243 Winter 2006 Stanford University 24 Iterated Dominance Frontier DF(SET) is the union of DF(X), where X є SET. DF(SET) is the union of DF(X), where X є SET. Iterated dominance frontier DF + (SET) is the limit of Iterated dominance frontier DF + (SET) is the limit of DF 1 = DF(SET) and DF i+1 = DF(SET U DF i ) DF 1 = DF(SET) and DF i+1 = DF(SET U DF i )

25 CS243 Winter 2006 Stanford University 25 Computing Joins J(SET) of join nodes J(SET) of join nodes Set of all nodes Z Set of all nodes Z There are two nonnull CFG paths that start at two distinct nodes in SET and converge at Z. There are two nonnull CFG paths that start at two distinct nodes in SET and converge at Z. Iterated join J + (SET) is the limit of Iterated join J + (SET) is the limit of J 1 = J(SET) and J i+1 = J(SET U J i ) J 1 = J(SET) and J i+1 = J(SET U J i ) J + (SET) = DF + (SET) J + (SET) = DF + (SET)

26 CS243 Winter 2006 Stanford University 26 Placing Functions Placing Ф Functions For each variable V For each variable V Add all nodes with assignments to V to worklist W Add all nodes with assignments to V to worklist W While X in W do While X in W do For each Y in DF(X) do For each Y in DF(X) do If no Ф added in Y then If no Ф added in Y then Place (V = Ф (V,…,V)) at Y Place (V = Ф (V,…,V)) at Y If Y has not been added before, add Y to W. If Y has not been added before, add Y to W.

27 CS243 Winter 2006 Stanford University 27 Computational Complexity Constructing SSA takes O(A tot * avrgDF), where Constructing SSA takes O(A tot * avrgDF), where A tot : total number of assignments A tot : total number of assignments avrgDF: weighted average DF size avrgDF: weighted average DF size The computational complexity is O(n 2 ). The computational complexity is O(n 2 ). e.g. nested repeat-until loops e.g. nested repeat-until loops S a b c E d

28 CS243 Winter 2006 Stanford University 28 Placement Example Ф Placement Example a = … a = Ф (a,a) Place Ф at Iterative Dominance Frontiers

29 CS243 Winter 2006 Stanford University 29 SSA Construction (II) Step 2: Rename all variables in original program and Ф functions, using dominator tree and rename stack to keep track of the current names. Step 2: Rename all variables in original program and Ф functions, using dominator tree and rename stack to keep track of the current names.

30 CS243 Winter 2006 Stanford University 30 Variable Renaming Rename from the START node recursively Rename from the START node recursively For node X For node X For each assignment (V = …) in X For each assignment (V = …) in X Rename any use of V with the TOS of rename stack Rename any use of V with the TOS of rename stack Push the new name V i on rename stack Push the new name V i on rename stack i = i + 1 i = i + 1 Rename all the Ф operands through successor edges Rename all the Ф operands through successor edges Recursively rename for all child nodes in the dominator tree Recursively rename for all child nodes in the dominator tree For each assignment (V = …) in X For each assignment (V = …) in X Pop V i in X from the rename stack Pop V i in X from the rename stack

31 CS243 Winter 2006 Stanford University 31 Renaming Example a1=a1= a 1 +5 a = a+5 a= a= Ф (a 1,a) a+5 TOS Rename expr

32 CS243 Winter 2006 Stanford University 32 Overview SSA Representation SSA Representation SSA Construction SSA Construction Converting out of SSA Converting out of SSA

33 CS243 Winter 2006 Stanford University 33 Converting Out of SSA Mapping all V i to V? Mapping all V i to V?

34 CS243 Winter 2006 Stanford University 34 Overlapping Live Ranges Simply mapping all V i to V may not work Simply mapping all V i to V may not work a 1 = b 1 a 1 +5 b2=b2= a 1 = b 1 b 1 +5 b2=b2= a 1 = b 1 b 1 +5 b2=b2= a 1 +5

35 CS243 Winter 2006 Stanford University 35 Converting Out of SSA Option 1: coloring Option 1: coloring Compute live ranges, and assign a unique variable name for each live range Compute live ranges, and assign a unique variable name for each live range Similar techniques used in register allocation to be covered next week. Similar techniques used in register allocation to be covered next week. Option 2: simply remove all Ф functions Option 2: simply remove all Ф functions Every optimization in SSA needs to guarantee not to generate overlapping live ranges Every optimization in SSA needs to guarantee not to generate overlapping live ranges

36 CS243 Winter 2006 Stanford University 36 Reference “Efficient Computing Static Single Assignment Form and the Control Dependence Graph”, R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and F. K. Zadeck, Transactions on Programming Languages and Systems (TOPLAS), Oct ently.html “Efficient Computing Static Single Assignment Form and the Control Dependence Graph”, R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and F. K. Zadeck, Transactions on Programming Languages and Systems (TOPLAS), Oct ently.html ently.html ently.html

37 CS243 Winter 2006 Stanford University 37 Backup

38 CS243 Winter 2006 Stanford University 38 Handling Arrays Difficult to treat A[i] as a variable Difficult to treat A[i] as a variable The entire array can be treated like a scalar. The entire array can be treated like a scalar. = 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 )

39 CS243 Winter 2006 Stanford University 39 Unnecessary Liveness W operator may introduce unnecessary liveness for A. Introduce HW (HiddenW). 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


Download ppt "Stanford University CS243 Winter 2006 Wei Li 1 SSA."

Similar presentations


Ads by Google