330 likes | 351 Views
Explore algorithms & efficiency measurements, analyze SSA for structured code, compute SSA for loops & conditionals in CS540 course material.
E N D
Static Single Assignment CS 540
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) CS540 Spring 2010
k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else m = k / 2 (7) endif (8) k = k + m k = 2 (2) if k(1) > 5 then (3) k = k(1)+ 1 (4) m = k(3)* 2 (5) else m = k(1) / 2 (7) endif (8) k = k(1,3)+ m(4,6) Consider the following The uses in each statement have been marked with the statement number of all definitions that reach. CS540 Spring 2010
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. CS540 Spring 2010
k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else m = k / 2 (7) endif (8) k = k + m k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else m2 = k1 / 2 (7) endif (8) k3 = k??+ m?? SSA numbering Problem: Because of multiple reaching definitions, we can’t give each use a unique number without analysis. CS540 Spring 2010
k = 2 (2) if k > 5 then (3) k = k + 1 (4) m = k * 2 (5) else m = k / 2 (7) Endif (8) k = k + m k1 = 2 (2) if k1 > 5 then (3) k2 = k1 + 1 (4) m1 = k2 * 2 (5) else m2 = k1 / 2 (7) Endif k3 = f (k1,k2) m3 = f (m1,m2) (8) k4 = k3+ m3 F Functions F functions - merge definitions, factoring in control flow CS540 Spring 2010
SSA for Structured Code Associate with each variable x, a current counter xc. Assignment statement: x := y op z becomes xxc++ := yyc op zzc CS540 Spring 2010
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 MMc++ := f (Mk,Mj); at loop start s = 1 s1 = 1 repeat repeat … … s3 = f(s1,s2) s = s + 1 s2 = s3 + 1 until s > 5 until s2 > 5 CS540 Spring 2010
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 MMc++ := f(Mk, Mj); after the conditional if … then if … then a := b aj := bn else else a := c ak := cm al =f(aj,ak) CS540 Spring 2010
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 MMc++ := f(Mk, Mj); after the conditional a := c aj := cm if … then if … then a := b ak := bn else … else … al =f(aj,ak) CS540 Spring 2010
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 MMc++ := f(Mk, Mj); after the conditional a := c aj := cm if … then if … then a := b ak := bn al =f(aj,ak) CS540 Spring 2010
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 CS540 Spring 2010
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 CS540 Spring 2010
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 = f() 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 f definitions where needed CS540 Spring 2010
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 = f(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 CS540 Spring 2010
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 = f(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 = f(j2,j3) print (i,j4,k,l) repeat if R then l = l + 4 until S i = i + 6 until T CS540 Spring 2010
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 = f(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 = f(k3,k4) print (i,j,k5,l) repeat if R then l = l + 4 until S i = i + 6 until T CS540 Spring 2010
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 = f(l1,l9) if (p) then begin j = i if Q then l3 = 2 else l4 = 3 l5 = f(l3,l4) k = k + 1 end else k = k + 2 l6 = f(l2,l5) print (i,j,k,l6) repeat l7 = f(l6,l9) if R then l8 = l7 + 4 l9 = f(l7,l8) until S i = i + 6 until T CS540 Spring 2010
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 = f(i1,i2) j2 = f(j1,j4) k2 = f(k5,k1) l2 = f(l9,l1) if (p) then begin j3 = i2 if Q then l3 = 2 else l4 = 3 l5 = f(l3,l4) k3 = k2 + 1 end else k4 = k2 + 2 j4 = f(j3,j2) k5 = f(k3,k4) l6 = f(l2,l5) print (i3,j4,k5,l6) repeat l7 = f(l9,l6) if R then l8 = l7 + 4 l9 = f(l7,l8) until S i2 = i3 + 6 until T CS540 Spring 2010
Using SSA for Constant Propagation • For statements xi := C, for some constant C, replace all xi with C and remove the statement • For xi := f(c,c,...,c), for some constant c, replace statement with xi := c • Can extend to evaluate conditional branches • Iterate Locates AND “Performs” the replacement
Example: SSA a1 := 3 d1 := 2 a := 3 d := 2 d3 = f(d1,d2) a3 = f(a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 f := a + d g := 5 a := g – d f < = g F T F T f2 := g1 + 1 g1 < a2 f := g + 1 g < a T T F F f3 := f(f1,f2) d2 := 2 d := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = f(d1,d2) a3 = f(a1,a2) f1 := a3 + d3 g1 := 5 a2 := g1 – d3 f1 <= g1 d3 = f(2,2) a3 = f(3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 F F T T f2 := g1 + 1 g1 < a2 f2 := 5 + 1 5 < a2 T T F F f3 := f(f1,f2) d2 := 2 f3 := f(f1,f2) d2 := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = f(2,2) a3 = f(3,a2) f1 := a3 + d3 g1 := 5 a2 := 5 – d3 f1 <= 5 d3 = 2 a3 = f(3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 F F T T f2 := 5 + 1 5 < a2 f2 := 6 5 < a2 T T F F f3 := f(f1,f2) d2 := 2 f3 := f(f1,6) d2 := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = 2 a3 = f(3,a2) f1 := a3 + 2 g1 := 5 a2 := 5 – 2 f1 <= 5 d3 = 2 a3 = f(3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 F F T T f2 := 6 5 < a2 f2 := 6 5 < 3 T T F F f3 := f(f1,6) d2 := 2 f3 := f(f1,6) d2 := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = 2 a3 = f(3,3) f1 := a3 + 2 g1 := 5 a2 := 3 f1 <= 5 d3 = 2 a3 = 3 f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5 F F T T f2 := 6 5 < 3 f2 := 6 T F f3 := f(f1,6) d2 := 2 f3 := f(f1,6) d2 := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = 2 a3 = 3 f1 := 3 + 2 g1 := 5 a2 := 3 f1 <= 5 d3 = 2 a3 = 3 f1 := 3 + 2 g1 := 5 a2 := 3 true F T f2 := 6 f2 := 6 f3 := f(f2) d2 := 2 f3 := f(6) d2 := 2
Example: SSA a1 := 3 d1 := 2 a1 := 3 d1 := 2 d3 = 2 a3 = 3 f1 := 5 g1 := 5 a2 := 3 true d3 = 2 a3 = 3 f1 := 3 + 2 g1 := 5 a2 := 3 f2 := 6 f2 := 6 f3 := f(6) d2 := 2 d2 := 2
... X17 x10 ... X17 x11 X17Ø(x10,x11) ... x17 ... x17 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 CS540 Spring 2010 *
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 k0 = 1 k1 = k0 1 2 k1 = f(k0,k4) 3 4 5 7 k3 =k1 + 2 k4 = k3 6 k2 =k1 + 1 k4 = k2 8 k4 = f(k2,k3) 9 10 11 Exit 12 CS540 Spring 2010 k1 =k4
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 CS 540 Spring 2010 GMU
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 CS 540 Spring 2010 GMU
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 CS 540 Spring 2010 GMU
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. CS 540 Spring 2010 GMU