Download presentation
Presentation is loading. Please wait.
Published byBlaze Dennis Modified over 9 years ago
1
Computer Science 313 – Advanced Programming Topics
2
Knuth's Rules Of Optimization
5
Problem We Face
7
Static Single Assignment Ignore programmer since they are dumb Go through and rewrite all local variables NOT 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
8
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 (…) { d = 2; } else { c = 3; } b = a + 1; if (…) { d 1 = 2; } else { c 1 = 3; } b 1 = a 1 + 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 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 ;
11
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 ;
12
Example of a CFG + SSA 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 ;
13
Limits 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 5 = a 2 + 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 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
15
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 ;
16
Dominators X dominates Y if and only if ALL PATHS X on ALL PATHS to Y Must find for good time on weekends
17
Dominators X dominates Y if and only if ALL PATHS X on ALL PATHS to Y Must find for good time on weekends
18
Dominators X dominates Y if and only if ALL PATHS X on ALL PATHS to Y Each basic block needs this to convert to SSA
19
Dominators X dominates Y if and only if ALL PATHS X on ALL PATHS to Y Each basic block needs this to convert to SSA
20
Dominators X dominates Y if and only if ALL PATHS 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 Y if and only if ALL PATHS 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 a b c d END START CFG DT
23
Dominator Tree Example START a b c d END START CFG DT a
24
Dominator Tree Example START a b c d END START CFG DT a b c
25
Dominator Tree Example START a b c d END START CFG DT a b c d
26
Dominator Tree Example START a b c d END START CFG DT a b c d 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 AND X dominates predecessor of Y AND X 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 AND X dominates predecessor of Y AND X 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 ’s dominance frontier runner dominatorTree. parent (b)
30
DF Example START a bc d END CFG DT START a bc 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)
31
DF Example START a bc d END CFG DT START a bc 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)
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 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.