350 likes | 448 Views
Computer Science 313 – Advanced Programming Topics. Lecture 2: SSA Form. Knuth's Rules Of Optimization. NO!. Knuth's Rules Of Optimization. NO! Not yet!. Knuth's Rules Of Optimization. NO! Not yet! For experts only!. Problem We Face. Want to optimize code , but face problems:
E N D
Computer Science 313 – Advanced Programming Topics Lecture 2:SSA Form
Knuth's Rules Of Optimization NO! Not yet!
Knuth's Rules Of Optimization NO! Not yet! For experts only!
Problem We Face • Want to optimize code, but face problems: Programmers are stupid • Code awful: contains variable reassigned regularly • Hard to know value at any single point in code • Would be much easier if many variables used
Problem We Face • Want to optimize code, but face problems: Programmers are human • Maintenance & readabilityprobably important • But only for humans; compiler wants to go zooom
Static Single Assignment • Ignore programmer since they are dumb • Go through and rewrite all local variables • But NOTfields (other threads may use them) • Change code so each variable assigned once (def) • Tie value (use) to definition for that line of code • Names unimportant, since only used by compiler • Optimizations easier & need not fix humans
Examples of SSA Form a = 2; b = a + 1; a = 3; b = a + 1; a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (…) {d1= 2;} else {c1= 3;} b1= a1+ 1; if (…) {d= 2;} else {c = 3;} b = a + 1;
Control Flow Graph • Common technique showing program structure • Visualizes execution paths possible in a method • Also referred to as flow of a program’s control • Vertices are “basic blocks” found in method • Basic block is code that must be executed together • Edges represent transfer of flow between blocks • Normally result from start & end of loops or branches • Goes to block could come next during a method run
Example of a CFG a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (b2 > 20) { System.out.println(“Woot”); } else { System.err.print(“Doh”); foo(a2); } b3 = a2+ b2 ;
Example of a CFG a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (b2 > 20) F T System.err.print(“Woot”); System.err.print(“Doh”); foo(a2); b3 = a2+ b2;
Example of a CFG + SSA a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (b2 > 20) F T System.err.print(“Woot”); System.err.print(“Doh”); foo(a2); b3 = a2 + b2;
Limits of SSA Form a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (b2 > 20) F T b3 = 21 foo(a2); b5 = a2 + b????;
Ф Functions Are Fun! • SSA has problems when code branches • What if variable assigned – cannot know defs to use • Ф function merges values along multiple paths • Ф function has one input for each incoming vertex • 1def for all future useof variable now exists • Remember that Ф function does not exist • Simple bookkeeping to allow SSA to work • Compiler also uses to track related values
Examples of SSA Form a1 = 2; b1 = a1 + 1; a2 = 3; b2 = a2 + 1; if (b2 > 20) F T b3 = 21 foo(a2); b4 = Ф (b3, b2); b5 = a2 + b4;
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Must find for good time on weekends
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Must find for good time on weekends
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA • Reflexive & transitive & fun relation defined • dom(Y)defines dominators of Y • To know when to add Ф nodes for Y use dom(Y) • Must be computed
Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA • Reflexive & transitive & fun relation defined • dom(Y)defines dominators of Y • To know when to add Ф nodes for Y use dom(Y) • Must be computed (unless you’ve been a bad coder)
Dominator Tree Example START START a c b d CFG DT END
Dominator Tree Example START START a a c b d CFG DT END
Dominator Tree Example START START a a b c c b d CFG DT END
Dominator Tree Example START START a a d b c c b d CFG DT END
Dominator Tree Example START START END a a d b c c b d CFG DT END
Next Step for SSA Form • Dominance frontier for node X in CFG • Defines set such that for each node Y in the d.f.: X != Y ANDXdominates predecessor of Y ANDX does not dominate Y
Next Step for SSA Form • Dominance frontier for node X in CFG • Defines set such that for each node Y in the d.f.: X != Y ANDX dominates predecessor of Y ANDX does not dominate Y
DF Computation • Algorithm to compute dominance frontier for (Vertex b : CFG.vertices()) if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.hasEdgeTargetting(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add runner to b’sdominance frontier runner dominatorTree.parent(b)
DF Example START START a a END DT d b c b c for (Vertex b : CFG.vertices())if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.hasEdgeTargetting(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier • runner dominatorTree.parent(runner) d CFG END
DF Example START START a a END DT d b c b c for (Vertex b : CFG.vertices())if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.hasEdgeTargetting(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier • runner dominatorTree.parent(runner) d CFG END
Placing Φ Nodes • If a basic block X has assignment to variable a • May need Φ function for a but where to place it? • Add Φ to all blocks with X in dominance frontier • Repeat algorithm for each assignment & block • No shortcuts here: must compute iteratively • Quick for computer to do & so can spare the whip
Placing Φ Nodes • If a basic block X has assignment to variable a • May need Φ function for a but where to place it? • Add Φ to all blocks with X in dominance frontier • Repeat algorithm for each assignment & block • No shortcuts here: must compute iteratively • Quick for computer to do & so can spare the whip
Why Should You Care? • Do you understand how programs optimized? • Many common beliefs wrong & ridiculous • Avoid looking like poseur & write useless speedups • May confuse compiler and prevent real optimizations
For Next Lecture • Read pages 26 - 32 • What do you when musttalk about a program? • What do you document (when it matters?) • What do wish others documentation would say? • Good design means what? Why? How do you know? • Ever seen beautiful code? What made it pretty?