Lecture 2: SSA Form
description
Transcript of Lecture 2: SSA Form
LECTURE 2:SSA FORM
Computer Science 313 – Advanced Programming Topics
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:
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 & readability probably 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 NOT fields (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 (…) { d = 2;} else { c = 3;}b = a + 1;
if (…) { d1 = 2;} else { c1 = 3;}b1 = a1 + 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)
System.err.print(“Woot”);
System.err.print(“Doh”);foo(a2);
T F
b3 = a2 + b2;
Example of a CFG + SSA
a1 = 2;b1 = a1 + 1;a2 = 3;b2 = a2 + 1;if (b2 > 20)
System.err.print(“Woot”);
System.err.print(“Doh”);foo(a2);
T F
b3 = a2 + b2;
Limits of SSA Form
a1 = 2;b1 = a1 + 1;a2 = 3;b2 = a2 + 1;if (b2 > 20)
b3 = 21 foo(a2);
T F
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 1 def for all future use of 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)
b3 = 21 foo(a2);
T F
b4 = Ф (b3, b2);b5 = a2 + b4;
Dominators
X dominates Y if and only if X on ALL PATHS to Y
Must find for good time on weekends
Dominators
X dominates Y if and only if X on ALL PATHS to Y
Must find for good time on weekends
Dominators
X dominates Y if and only if X on ALL PATHS to Y
Each basic block needs this to convert to SSA
Dominators
X dominates Y if and only if X on ALL PATHS to Y
Each basic block needs this to convert to SSA
Dominators
X dominates Y if 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 Y if 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
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 such that for each node Y in
the d.f.:
X != Y AND
X dominates predecessor of Y AND
X 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 AND
X dominates predecessor of Y AND
X does not dominate Y
DF Computation
Algorithm to compute dominance frontierfor (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’s dominance frontier runner dominatorTree.parent(b)
DF Example
START
a
b c
d
ENDCFG
DT
START
a
b c d
END
for (Vertex b : CFG.vertices())
if (CFG.countInEdges(b) ≥ 2)
for (Vertex p : CFG.hasEdgeTargetting(b))
runner p
while (runner != dominatorTree.parent(b))
// Add b to runner’s dominance frontier runner dominatorTree.parent(runner)
DF Example
START
a
b c
d
ENDCFG
DT
START
a
b c d
END
for (Vertex b : CFG.vertices())
if (CFG.countInEdges(b) ≥ 2)
for (Vertex p : CFG.hasEdgeTargetting(b))
runner p
while (runner != dominatorTree.parent(b))
// Add b to runner’s dominance frontier runner dominatorTree.parent(runner)
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 must talk 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?