# Computer Science 313 – Advanced Programming Topics.

## Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

Computer Science 313 – Advanced Programming Topics

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; a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (…) { a = 2; } else { a = 3; } b = a + 1; if (…) { a 1 = 2; } else { a 2 = 3; } a 3 = Φ(a 1, a 2 ); b = a 3 + 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 a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) { System.out.println(“Woot”); } else { System.err.print(“Doh”); foo(a 2 ); } b 3 = a 2 + b 2

Example of a CFG a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) System.err.print(“Woot”); System.err.print(“Doh”); foo(a 2 ); T F b 3 = a 2 + b 2 ;

Ф 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 a 1 = 2; b 1 = a 1 + 1; a 2 = 3; b 2 = a 2 + 1; if (b 2 > 20) b 3 = 21 foo(a 2 ); T F b 4 = Ф (b 3, b 2 ); b 5 = a 2 + b 4 ;

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 –trix suffix  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 a b c d END START CFG DT

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

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

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

Dominator Tree Example START a b c d END START CFG DT a b c d 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 and X != Y

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 b to runner ’s dominance frontier runner  dominatorTree. parent (b)

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

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

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

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 b to 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 ’s dominance 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