1 / 35

Lecture 2: SSA Form

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:

royal
Download Presentation

Lecture 2: SSA Form

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 2:SSA Form

  2. Knuth's Rules Of Optimization NO!

  3. Knuth's Rules Of Optimization NO! Not yet!

  4. Knuth's Rules Of Optimization NO! Not yet! For experts only!

  5. 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

  6. 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

  7. 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

  8. 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;

  9. 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

  10. 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 ;

  11. 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;

  12. 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;

  13. 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????;

  14. Ф 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

  15. 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;

  16. Dominators • X dominates Yif and only if X on ALL PATHS to Y • Must find for good time on weekends

  17. Dominators • X dominates Yif and only if X on ALL PATHS to Y • Must find for good time on weekends

  18. Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA

  19. Dominators • X dominates Yif and only if X on ALL PATHS to Y • Each basic block needs this to convert to SSA

  20. 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

  21. 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)

  22. Dominator Tree Example START START a c b d CFG DT END

  23. Dominator Tree Example START START a a c b d CFG DT END

  24. Dominator Tree Example START START a a b c c b d CFG DT END

  25. Dominator Tree Example START START a a d b c c b d CFG DT END

  26. Dominator Tree Example START START END a a d b c c b d CFG DT END

  27. 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

  28. 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

  29. DF Computation • Algorithm to compute dominance frontier for (Vertex b : CFG.vertices()) if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.hasEdgeTargetting(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add runner to b’sdominance frontier runner  dominatorTree.parent(b)

  30. 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)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier • runner  dominatorTree.parent(runner) d CFG END

  31. 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)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier • runner  dominatorTree.parent(runner) d CFG END

  32. 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

  33. 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

  34. 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

  35. 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?

More Related