220 likes | 469 Vues
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)
E N D
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) • Variable use tied to definition at the assignment
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;
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
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;
Ф 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
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 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)
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 of nodes such that each node Y in set • X dominates predecessor of Y, but • X does not dominate Y andX != Y
DF Computation • Algorithm to compute dominance frontier for (Vertex b : CFG.vertices()) if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.adjacentVertices(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier runner dominatorTree.parent(b)
DF Example START START a END a c b b d c DT d DF(c) = ? CFG END
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
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
DF Computation • Algorithm to compute dominance frontier for (Vertex b : CFG.vertices()) if (CFG.countInEdges(b) ≥ 2) for (Vertex p: CFG.adacentVertices(b)) runnerp while (runner!= dominatorTree.parent(b)) // Add bto runner’s dominance frontier runner dominatorTree.parent(b)
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
For Next Class • Lab #6 available on Angel • Will start to look at how SSA can be used