Lecture 2: SSA Form

35
LECTURE 2: SSA FORM Computer Science 313 – Advanced Programming Topics

description

Computer Science 313 – Advanced Programming Topics. Lecture 2: SSA Form. 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: - PowerPoint PPT Presentation

Transcript of Lecture 2: SSA Form

Page 1: Lecture 2: SSA Form

LECTURE 2:SSA FORM

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 2: SSA Form

Knuth's Rules Of Optimization

NO!

Page 3: Lecture 2: SSA Form

Knuth's Rules Of Optimization

NO!

Not yet!

Page 4: Lecture 2: SSA Form

Knuth's Rules Of Optimization

NO!Not yet!

For experts only!

Page 5: Lecture 2: SSA Form

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

Page 6: Lecture 2: SSA Form

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

Page 7: Lecture 2: SSA Form

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

Page 8: Lecture 2: SSA Form

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;

Page 9: Lecture 2: SSA Form

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

Page 10: Lecture 2: SSA Form

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 ;

Page 11: Lecture 2: SSA Form

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;

Page 12: Lecture 2: SSA Form

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;

Page 13: Lecture 2: SSA Form

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????;

Page 14: Lecture 2: SSA Form

Ф 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

Page 15: Lecture 2: SSA Form

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;

Page 16: Lecture 2: SSA Form

Dominators

X dominates Y if and only if X on ALL PATHS to Y

Must find for good time on weekends

Page 17: Lecture 2: SSA Form

Dominators

X dominates Y if and only if X on ALL PATHS to Y

Must find for good time on weekends

Page 18: Lecture 2: SSA Form

Dominators

X dominates Y if and only if X on ALL PATHS to Y

Each basic block needs this to convert to SSA

Page 19: Lecture 2: SSA Form

Dominators

X dominates Y if and only if X on ALL PATHS to Y

Each basic block needs this to convert to SSA

Page 20: Lecture 2: SSA Form

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

Page 21: Lecture 2: SSA Form

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)

Page 22: Lecture 2: SSA Form

Dominator Tree Example

START

a

b c

d

END

START

CFG DT

Page 23: Lecture 2: SSA Form

Dominator Tree Example

START

a

b c

d

END

START

CFG DT

a

Page 24: Lecture 2: SSA Form

Dominator Tree Example

START

a

b c

d

END

START

CFG DT

a

b c

Page 25: Lecture 2: SSA Form

Dominator Tree Example

START

a

b c

d

END

START

CFG DT

a

b c d

Page 26: Lecture 2: SSA Form

Dominator Tree Example

START

a

b c

d

END

START

CFG DT

a

b c d

END

Page 27: Lecture 2: SSA Form

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

Page 28: Lecture 2: SSA Form

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

Page 29: Lecture 2: SSA Form

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)

Page 30: Lecture 2: SSA Form

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)

Page 31: Lecture 2: SSA Form

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)

Page 32: Lecture 2: SSA Form

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

Page 33: Lecture 2: SSA Form

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

Page 34: Lecture 2: SSA Form

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

Page 35: Lecture 2: SSA Form

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?