1 / 22

Lecture 23: SSA Form & Common Subexpression Elim.

Computer Science 313 – Advanced Programming Topics. Lecture 23: SSA Form & Common Subexpression Elim. Static Single Assignment. Ignores local variables programmer wrote Programmers are stupid Creates lots of variables on its own Each variable is assigned exactly once (def)

huong
Télécharger la présentation

Lecture 23: SSA Form & Common Subexpression Elim.

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 23:SSA Form & Common Subexpression Elim.

  2. Static Single Assignment • Ignores local variables programmer wrote • Programmers are stupid • Creates lots of variables on its own • Each variable is assigned exactly once (def) • Variable use tied to definition at the assignment

  3. 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 (…) { a1 = 2;} else { a2 = 3;}a3 = Φ(a1, a2); b = a3 + 1; if (…) { a = 2;} else { a = 3;} b = a + 1;

  4. Control Flow Graph • Common technique showing program structure • Visualizes different ways method can be run • Also referred to as the flow of a program’s control • Basic blocks are vertices in this graph • 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

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

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

  7. Ф Functions Are Fun! • SSA could have problem with basic blocks • What happens when variable defined in if & else • Uses Ф function to merge variable definitions • Ф function has one input per merged block • Variables have exactly one definition with function • Function does not really exist • Keeps value from the basic block executed • Keeps books properly balanced

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

  9. Dominators • X dominates Y if and only if X on all paths from method start to Y • Used to help convert code to SSA form • Excuse for explaining –trixsuffix • Domination both reflexive & transitive • dom(Y)defines dominators of Y • Set of definitions which reach a given block • Will create need to add Ф for Y using dom(Y)

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

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

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

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

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

  15. Next Step for SSA Form • Dominance frontier for node X in CFG • Defines set of nodes such that each node Y in set • X dominates predecessor of Y, but • X does not dominate Y andX != Y

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

  17. DF Example START START a END a c b b d c DT d DF(c) = ? CFG END

  18. DF Example START START a END a c b b d c DT d DF(c) = {d} c dominates c,but not d CFG END

  19. DF Example START START a END a c b b d c DT d DF(c) = {d} DF(a) = {END} a dominates b,c,&d, but not END CFG END

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

  21. Placing Φ Nodes • If basic block, x, defines variable named a • Need Φ function for a at start of all blocks where • Block is in x’sdominance frontier –or– • In dominance frontier of block in x’s dominance frontier • Repeat algorithm however long as needed • Must be computed iteratively

  22. For Next Class • Lab #6 available on Angel • Will start to look at how SSA can be used

More Related