Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Single Assignment CS 540. Spring 2010 2 Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.

Similar presentations


Presentation on theme: "Static Single Assignment CS 540. Spring 2010 2 Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation."— Presentation transcript:

1 Static Single Assignment CS 540

2 Spring 2010 2 Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation in how easy it is to use, and how easy it is to generate. Static Single Assignment (SSA)

3 CS540 Spring 2010 3 Consider the following (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) endif (8) k = k + m (1)k = 2 (2) if k (1) > 5 then (3) k = k (1) + 1 (4) m = k (3) * 2 (5) else (6) m = k (1) / 2 (7) endif (8) k = k (1,3) + m (4,6) The uses in each statement have been marked with the statement number of all definitions that reach.

4 CS540 Spring 2010 4 Static Single Assignment Idea: Each definition will be uniquely numbered. There will be a single reaching definition for each point. Algorithms for static single assignment are space efficient and take control flow into account.

5 CS540 Spring 2010 5 SSA numbering (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) endif (8) k = k + m (1)k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else (6) m2 = k1 / 2 (7) endif (8) k3 = k??+ m?? Problem: Because of multiple reaching definitions, we can’t give each use a unique number without analysis.

6 CS540 Spring 2010 6  Functions (1)k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else (6) m = k / 2 (7) Endif (8) k = k + m (1)k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else (6) m2 = k1 / 2 (7) Endif k3 =  (k1,k2) m3 =  (m1,m2) (8) k4 = k3+ m3  functions - merge definitions, factoring in control flow

7 CS540 Spring 2010 7 SSA for Structured Code Associate with each variable x, a current counter xc. Assignment statement: x := y op z becomes x xc++ := y yc op z zc

8 CS540 Spring 2010 8 SSA for Structured Code Loops: Repeat S until E for all variables M with definition k in loop body if M has a definition j above the loop then generate M Mc++ := f (M k,M j ); at loop start s = 1s 1 = 1repeat … … s 3 =  (s 1,s 2 ) s = s + 1 s 2 = s 3 + 1 until s > 5until s 2 > 5

9 CS540 Spring 2010 9 Computing SSA for Structured Code Conditionals: –if E then S1 else S2 for all variables M with definition in either S1 or S2 Case 1: M has definition j in S1 and definition k in S2 Generate M Mc++ :=  (M k, M j ); after the conditional if … then a := b a j := b n else a := c a k := c m a l =  (a j,a k )

10 CS540 Spring 2010 10 Computing SSA for Structured Code Conditionals: –if E then S1 else S2 for all variables M with definition in either S1 or S2 Case 2: M has definition k in S1 or in S2, definition j above the conditional Generate M Mc++ :=  (M k, M j ); after the conditional a := ca j := c m if … then a := b a k := b n else …else … a l =  (a j,a k )

11 CS540 Spring 2010 11 Computing SSA for Structured Code Conditionals: –if E then S1 for all variables M with definition k in S1 and definition j that reaches the conditional, generate M Mc++ :=  (M k, M j ); after the conditional a := ca j := c m if … then a := b a k := b n a l =  (a j,a k )

12 CS540 Spring 2010 12 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T

13 CS540 Spring 2010 13 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i2 = i + 6 until T Number existing defns

14 CS540 Spring 2010 14 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat i3 =  () if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i2 = i + 6 until T Add  definitions where needed

15 CS540 Spring 2010 15 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j = k = l = 1 repeat i3 =  (i1,i2) if (p) then begin j = i3 if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i3,j,k,l) repeat if R then l = l + 4 until S i2 = i3 + 6 until T Fill in the use numbers

16 CS540 Spring 2010 16 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j1 = k = l = 1 repeat j2 =  (j1,j4) if (p) then begin j3 = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 j4 =  (j2,j3) print (i,j4,k,l) repeat if R then l = l + 4 until S i = i + 6 until T

17 CS540 Spring 2010 17 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j = k1 = l = 1 repeat k2 =  (k1,k5) if (p) then begin j = i if Q then l = 2 else l = 3 k3 = k2 + 1 end else k4 = k2 + 2 k5 =  (k3,k4) print (i,j,k5,l) repeat if R then l = l + 4 until S i = i + 6 until T

18 CS540 Spring 2010 18 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i = j = k = l1 = 1 repeat l2 =  (l1,l9) if (p) then begin j = i if Q then l3 = 2 else l4 = 3 l5 =  (l3,l4) k = k + 1 end else k = k + 2 l6 =  (l2,l5) print (i,j,k,l6) repeat l7 =  (l6,l9) if R then l8 = l7 + 4 l9 =  (l7,l8) until S i = i + 6 until T

19 CS540 Spring 2010 19 i = j = k = l = 1 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k = k + 1 end else k = k + 2 print (i,j,k,l) repeat if R then l = l + 4 until S i = i + 6 until T i1 = j1 = k1 = l1 = 1 repeat i3 =  (i1,i2) j2 =  (j1,j4) k2 =  (k5,k1) l2 =  (l9,l1) if (p) then begin j3 = i2 if Q then l3 = 2 else l4 = 3 l5 =  (l3,l4) k3 = k2 + 1 end else k4 = k2 + 2 j4 =  (j3,j2) k5 =  (k3,k4) l6 =  (l2,l5) print (i3,j4,k5,l6) repeat l7 =  (l9,l6) if R then l8 = l7 + 4 l9 =  (l7,l8) until S i2 = i3 + 6 until T

20 Using SSA for Constant Propagation For statements x i := C, for some constant C, replace all x i with C and remove the statement For x i :=  (c,c,...,c), for some constant c, replace statement with x i := c Can extend to evaluate conditional branches Iterate Locates AND “Performs” the replacement

21 Example: SSA a := 3 d := 2 f := a + d g := 5 a := g – d f < = g f := g + 1 g < a d := 2 T F T F a1 := 3 d1 := 2 d3 =  (d1,d2) a3 =  (a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 f2 := g1 + 1 g1 < a2 f3 :=  (f1,f2) d2 := 2 T F T F

22 Example: SSA a1 := 3 d1 := 2 d3 =  (d1,d2) a3 =  (a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 f2 := g1 + 1 g1 < a2 f3 :=  (f1,f2) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  (2,2) a3 =  (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 f2 := 5 + 1 5 < a2 f3 :=  (f1,f2) d2 := 2 T F T F

23 Example: SSA a1 := 3 d1 := 2 d3 =  (2,2) a3 =  (3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 f2 := 5 + 1 5 < a2 f3 :=  (f1,f2) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  (3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 f2 := 6 5 < a2 f3 :=  (f1,6) d2 := 2 T F T F

24 Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  (3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 f2 := 6 5 < a2 f3 :=  (f1,6) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  (3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 5 < 3 f3 :=  (f1,6) d2 := 2 T F T F

25 Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  (3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 5 < 3 f3 :=  (f1,6) d2 := 2 T F T F a1 := 3 d1 := 2 d3 =  a3 =  f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 f3 :=  (f1,6) d2 := 2 T F

26 Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5 f2 := 6 f3 :=  (f2) d2 := 2 T F a1 := 3 d1 := 2 d3 =  a3 =  f1 := 3 + 2 g1 := 5 a2 := 3 true f2 := 6 f3 :=  (6) d2 := 2

27 Example: SSA a1 := 3 d1 := 2 d3 =  a3 =  f1 := 5 g1 := 5 a2 := 3 true f2 := 6 f3 :=  (6) d2 := 2 a1 := 3 d1 := 2 d3 =  a3 =  f1 := 3 + 2 g1 := 5 a2 := 3 f2 := 6 d2 := 2

28 CS540 Spring 2010 28 SSA Deconstruction At some point, we need executable code Can’t implement Ø operations Need to fix up the flow of values Basic idea Insert copies Ø-function pred’s Simple algorithm –Works in most cases Adds lots of copies –Most of them coalesce away X 17  Ø(x 10,x 11 )...  x 17......  x 17 X 17  x 10 X 17  x 11 *

29 CS540 Spring 2010 29           Exit k0 =  k1 = k0 k4 =  (k2,k3) k1 =  (k0,k4) k3 =  k1 + 2 k4 = k3 k2 =  k1 + 1 k4 = k2 i = j = k0 = l = 1 k1 = k0 repeat if (p) then begin j = i if Q then l = 2 else l = 3 k2 = k2 + 1 k4 = k2 end else k3 = k1 + 2 k4 = k3 print (i,j,k4,l) repeat if R then l = l + 4 until S i = i + 6 k1 = k4 until T k1 =  k4

30 CS 540 Spring 2010 GMU30 Final Exam 75%-80% (ish) on material since the midterm –Syntax directed translation (a little of this on midterm) –Symbol table & types –Intermediate code –Runtime Environments –Code Generation –Code Optimization Remaining – HL concepts from the first part of the semester

31 CS 540 Spring 2010 GMU31 Final Exam: Syntax directed translation & Types SDT: –Some on midterm already –Got lots of practice (program #3, #4) –Understanding/Creating Symbol Tables & Types –Types Terminology –Scope –Table implementation

32 CS 540 Spring 2010 GMU32 Final Exam: Intermediate Code & RT Environments Intermediate Code –Expressions, Control constructs –Should be able to write/understand basic code –Don’t memorize spim – will put info on exam RT Environments –Control flow –Data flow –Variable addressing –Parameter passing

33 CS 540 Spring 2010 GMU33 Final Exam: Code Generation & Code Optimization Code Generation –Instr. selection/Instruction scheduling: only what they are trying to accomplish –Register allocation: understand how to use liveness to allocation, graph coloring to assign –Review the example on the slides – could get a question like that Code Optimization –Gave lots of examples of optimizations that are useful –Dataflow analysis basics – I won’t make you use the equations (would take too long) but you should have a general idea what is being done.


Download ppt "Static Single Assignment CS 540. Spring 2010 2 Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation."

Similar presentations


Ads by Google