SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

47
SSA

Transcript of SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Page 1: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA

Page 2: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Agenda

• SSA Introduction

• Converting to SSA

• Converting out of SSA

• SSA Example

Page 3: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• SSA is a intermediate representation.• SSA was developed by Ron Cytron, Jeanne Ferr

ante, Barry Rosen, Mark Wegman, and Ken Zadeck, researchers at IBM in the 1980s.

• Benefits– constant propagation– sparse conditional constant propagation– dead code elimination– global value numbering– partial redundancy elimination– strength reduction– register allocation

Page 4: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• Static Single Assignment Form– Each variable has only one reaching definition.– When two definitions merge, a Ф function is in

troduced to with a new definition of the variable.

– A Ф operand represents the reaching definition from the corresponding predecessor.

Page 5: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

a1=

= a1+5

a2=

= a2+5

a3=

a4= Ф (a1,a3)

= a4+5

a =

= a+5

a =

= a+5

a =

= a+5

Page 6: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• SSA Condition– If two nonnull paths X →+ Z and Y →+ Z converge at a node, a

nd nodes X and Y contain assignments to V (in the original program), then a trivial -function V = (V,…,V) has been inserted at Z (in the new program)

– Each mention of V in the original program or in an inserted -function has been replaced by a mention of a new variable Vi, leaving the new program in SSA form

– Along any control flow path, consider any use of a variable V (in the original program) and the corresponding use of Vi (in the new program). Then V and Vi has same value.

Page 7: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• Where to insert -functions?– By condition 1, a node Z needs a -function fo

r V because Z is a convergence point for two nonnull paths X →+ Z and Y →+ Z that start at nodes X and Y already containing assignments to V.

Page 8: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• What about arrays?– Treating A[i] as a variable would be awkward,

both because an assignment to A[i] may or may not change the value of A[j] and because the value of A[i] could be changed by assigning to i rather than to A[i].

– Treat entire array as a scalar variable.

= A[i]A[j] = V = A[k]

= R(A,i)A = W(A,j,V) = R(A,k)

= R(A8,i7)A9 = W(A8,j6,V5) = R(A9,k4)

Page 9: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Introduction

• W operator may introduce unnecessary liveness for A. Introduce HW (HiddenW).

repeat

A[i] = i i = i +1until i>10

repeat i2 = Ф(i1,i3) A1 = Ф(A0,A2) A2= W(A1,i2,i2) i3 = i2 +1until i3>10

repeat i2 = Ф(i1,i3) A2= HW(i2,i2) i3 = i2 +1until i3>10

Page 10: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Converting to SSA

• Big picture, translation to SSA form is done in 3 steps– The dominance frontier mapping is

constructed form the control flow graph.– Using the dominance frontiers, the locations

of the -functions for each variable in the original program are determined.

– The variables are renamed by replacing each mention of an original variable V by an appropriate mention of a new variable Vi

Page 11: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

CFG

• A control flow graph G = (V, E)

• Set V contains distinguished nodes ENTRY and EXIT– every node is reachable from ENTRY– EXIT is reachable from every node in G. – ENTRY has no predecessors– EXIT has no successors.

• Notation: predecessor, successor, path

Page 12: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dominance Relation

• If X appears on every path from ENTRY to Y, then X dominates Y.

• Dominance relation is both reflexive and transitive.

• idom(Y): immediate dominator of Y• Dominator Tree

– ENTRY is the root– Any node Y other than ENTRY has idom(Y) as its par

ent– Notation: parent, child, ancestor, descendant

Page 13: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dominator Tree Example

ENTRY

a

b c

d

EXIT

ENTRY

a

d

EXIT

b c

CFG DT

Page 14: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dominator Tree Example

Page 15: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dominance Frontier

• Dominance Frontier DF(X) for node X– Set of nodes Y – X dominates a predecessor of Y– X does not strictly dominate Y

• Equation 1: DF(X) = {Y|(P Pred(Y))(X∈ P and XY )}

• Equation 2: DF(X) = DFlocal(X) ∪ ∪Z Children(X)∈ DFup(z)

• DFlocal(X) = {Y Succ(x)|idom(Y)≠X}∈• DFup(X) = {Y DF(Z)|idom(Z) ≠X}∈

Page 16: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dominance Frontier

• How to proof equation 1 and equation 2 are correct?– Easy to See that => is correct– Still have to show everything in DF(X) has been acco

unted for.– Suppose Y DF(X) and U∈ Y be the edge that X domi

nate U but doesn’t strictly dominate Y.– If U == X, then Y DF∈ local(X)– If U ≠X, then there exists a path from X to U in Domin

ator Tree which implies there exists a child Z of X dominate U. Z doesn’t strictly dominate Y because X doesn’t strictly dominate Y. So Y DF∈ up(X).

Page 17: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Ф-function and Dominance Frontier

• Intuition behind dominance frontier– Y DF(X) means:∈

• Y has multiple predecessors• X dominate one of them, say U, U inherits everythi

ng defined in X• Reaching definition of Y are from U and other pred

ecessors• So Y is exactly the place where Ф-function is need

ed

Page 18: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Control Dependences and Dominance Frontier

• A CFG node Y is control dependent on a CFG node X if both the following hold:– There is nonnull path p: X →+ Y such that Y p

ostdominate every node after X.– The node Y doesn’t strictly postdominate the

node X

• If X appears on every path from Y to Exit, then X postdominate Y.

Page 19: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Control Dependences and Dominance Frontier

• In other words, there is some edge from X that definitely causes Y to execute, and there is also some path from X that avoids executing Y.

Page 20: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

RCFG

• The reverse control flow graph RCFG has the same nodes as CFG, but has edge Y →X for each edge X→Y in CFG.

• Entry and Exit are also reversed.• The postdominator relation in CFG is domi

nator relation in RCFG.• Let X and Y be nodes in CFG. Then Y is c

ontrol dependent on X in CFG iff X DF(Y) ∈in RCFG.

Page 21: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Construction– Place Ф Functions

• For each variable V– Add all nodes with assignments to V to worklis

t W– While X in W do

• For each Y in DF(X) do– If no Ф added in Y then

» Place (V = Ф (V,…,V)) at Y» If Y has not been added before, add Y to W.

Page 22: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Construction– Rename Variables

• Rename from the ENTRY node recursively• For node X

– For each assignment (V = …) in X• Rename any use of V with the TOS of rename stack• Push the new name Vi on rename stack• i = i + 1

– Rename all the Ф operands through successor edges– Recursively rename for all child nodes in the dominat

or tree – For each assignment (V = …) in X

• Pop Vi in X from the rename stack

Page 23: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Rename Example

a1=

a1+5

a =

a+5

a=

a= Ф (a1,a)

a+5

TOS

Rename expr

a1

a0

Page 24: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Converting out of SSA

• Eventually, a program must be executed.

• The Ф-function have precise semantics, but they are generally not represented in existing target machines.

Page 25: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Converting Out of SSA

• Naively, a k-input Ф-function at entrance to node X can be replaced by k ordinary assignments, one at the end of each control predecessor of X.

• Inefficient object code can be generated.

Page 26: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Dead Code Elimination

• Where does dead code come from?– Assignments without any use

• Dead code elimination method– Initially all statements are marked dead– Some statements need to be marked live because of

certain conditions– Mark these statements can cause others to be marke

d live.– After worklist is empty, truly dead code can be remov

ed

Page 27: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Allocation By Coloring

• It might seem possible to map all occurrence of Vi back to V and delete all Ф-functions.

• After certain optimization, this assumption might not be true.

Page 28: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

Allocation By Coloring

• Use graph coloring techniques to allocate storage for variables.

• In the case above,V1 and V2 are alive simultaneously, they have different color, and will be converted back to two different variables.

Page 29: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

• Dead Code Elimination Intuition– Because there is only one definition for each

variable, if the list of uses of the variable is empty, the definition is dead.

– When a statement v x y is eliminated because v is dead, this statement must be removed from the list of uses of x and y. This might cause those definitions to become dead.

Page 30: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

• Simple Constant Propagation Intuition– If there is a statement v c, where c is a con

stant, then all uses of v can be replaced for c.– A function of the form v (c1, c2, …, cn)

where all ci are identical can be replaced for v c.

– Using a work list algorithm in a program in SSA form,we can perform constant propagation in linear time.

Page 31: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i 1j 1k 0

j ik k+1

j kk k+2

return jif j<20

if k<100

B1

B2

B3

B5 B6

B4

B7

Page 32: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i 1j 1k1 0

j ik3 k+1

j kk5 k+2

return jif j<20

if k<100

B1

B2

B3

B5 B6

B4

B7

Page 33: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i 1j 1k1 0

j ik3 k+1

j kk5 k+2

return jif j<20

if k<100

k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

Page 34: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i 1j 1k1 0

j ik3 k+1

j kk5 k+2

return jif j<20

k2 (k4,k1)if k<100

k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

Page 35: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i 1j 1k1 0

j ik3 k2+1

j kk5 k2+2

return jif j<20

k2 (k4,k1)if k2<100

k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

Page 36: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example

i=1;j=1;k=0; while(k<100) { if(j<20) { j=i; k=k+1; } else { j=k; k=k+2; } } return j;}

i1 1j1 1k1 0

j3 i1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,j1)k2 (k4,k1)if k2<100

j4 (j3,j5)k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

Page 37: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Constant Propagation

i1 1j1 1k1 0

j3 1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (j3,j5)k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

i1 1j1 1k1 0

j3 i1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,j1)k2 (k4,k1)if k2<100

j4 (j3,j5)k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

Page 38: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Dead Code Elimination

i1 1j1 1k1 0

j3 1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (j3,j5)k4 (k3,k5)

B1

B2

B3

B5 B6

B4

B7

j3 1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (j3,j5)k4 (k3,k5)

B2

B3

B5 B6

B4

B7

Page 39: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Constant Propagation and Dead Code Elimination

j3 1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (1,j5)k4 (k3,k5)

B2

B3

B5 B6

B4

B7

j3 1k3 k2+1

j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (j3,j5)k4 (k3,k5)

B2

B3

B5 B6

B4

B7

Page 40: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: One Step Further

But block 6 is neverexecuted! How can we

find this out, and simplifythe program?

SSA conditional constantpropagation finds the

least fixed point for theprogram and allows further elimination of

dead code.

k3 k2+1 j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (1,j5)k4 (k3,k5)

B2

B3

B5 B6

B4

B7

Page 41: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Dead Code Elimination

k3 k2+1 j5 k2k5 k2+2

return j2if j2<20

j2 (j4,1)k2 (k4,0)if k2<100

j4 (1,j5)k4 (k3,k5)

B2

B3

B6

B4

B7

B4

k3 k2+1

return j2

j2 (j4,1)k2 (k4,0)if k2<100

j4 (1)k4 (k3)

B2

B5

B7

Page 42: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Single Argument Function Elimination

B4

k3 k2+1

return j2

j2 (j4,1)k2 (k4,0)if k2<100

j4 1k4 k3

B2

B5

B7

B4

k3 k2+1

return j2

j2 (j4,1)k2 (k4,0)if k2<100

j4 (1)k4 (k3)

B2

B5

B7

Page 43: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Constant and Copy Propagation

k3 k2+1

return j2

j2 (j4,1)k2 (k4,0)if k2<100

j4 1k4 k3

B2

B5

B7

k3 k2+1

return j2

j2 (1,1)k2 (k3,0)if k2<100

j4 1k4 k3

B2

B5

B7

B4B4

Page 44: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: More Dead Code

k3 k2+1

return j2

j2 (1,1)k2 (k3,0)if k2<100

j4 1k4 k3

B2

B5

B7

B4

k3 k2+1

return j2

j2 (1,1)k2 (k3,0)if k2<100

B2

B5

B4

Page 45: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: More Function Simplification

k3 k2+1

return j2

j2 (1,1)k2 (k3,0)if k2<100

B2

B5

B4

k3 k2+1

return j2

j2 1k2 (k3,0)if k2<100

B2

B5

B4

Page 46: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: More Constant Propagation

k3 k2+1

return j2

j2 1k2 (k3,0)if k2<100

B2

B5

B4

k3 k2+1

return 1

j2 1k2 (k3,0)if k2<100

B2

B5

B4

Page 47: SSA. Agenda SSA Introduction Converting to SSA Converting out of SSA SSA Example.

SSA Example: Ultimate Dead Code Elimination

k3 k2+1

return 1

j2 1k2 (k3,0)if k2<100

B2

B5

B4 return 1 B4