stanford university cs243 winter 2006 wei li 1 ssa

39
Wei Li 1 Stanford University CS243 Winter 2006 SSA SSA

Upload: prince-meas

Post on 15-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stanford University CS243 Winter 2006 Wei Li 1 SSA

Wei Li 1Stanford University

CS243 Winter 2006

SSA SSA

Page 2: Stanford University CS243 Winter 2006 Wei Li 1 SSA

2CS243 Winter 2006Stanford University

OverviewOverview

SSA RepresentationSSA Representation SSA ConstructionSSA Construction Converting out of SSAConverting out of SSA

Page 3: Stanford University CS243 Winter 2006 Wei Li 1 SSA

3CS243 Winter 2006Stanford University

Static Single AssignmentStatic Single Assignment

Each variable has only one reaching Each variable has only one reaching definition.definition.

When two definitions merge, a When two definitions merge, a ФФ function function is introduced to with a new definition of the is introduced to with a new definition of the variable.variable.

First consider SSA for alias free variables.First consider SSA for alias free variables.

Page 4: Stanford University CS243 Winter 2006 Wei Li 1 SSA

4CS243 Winter 2006Stanford University

Example: CFGExample: CFG

a =

= a+5

a =

= a+5

a =

= a+5

Multiple reaching definitions

Page 5: Stanford University CS243 Winter 2006 Wei Li 1 SSA

5CS243 Winter 2006Stanford University

Example: SSA FormExample: SSA Form

a1=

= a1+5

a2=

= a2+5

a3=

a4= Ф(a1,a3)

= a4+5

Single reaching definition

Page 6: Stanford University CS243 Winter 2006 Wei Li 1 SSA

6CS243 Winter 2006Stanford University

ФФ Functions Functions A A ФФ operand represents the reaching operand represents the reaching

definition from the corresponding definition from the corresponding predecessor. predecessor.

The ordering of The ordering of ФФ operands are important operands are important for knowing from which path the definition for knowing from which path the definition is coming from.is coming from.

Page 7: Stanford University CS243 Winter 2006 Wei Li 1 SSA

7CS243 Winter 2006Stanford University

SSA ConditionsSSA Conditions

1.1. If two nonnull paths X If two nonnull paths X →→++ Z and Y → Z and Y →++ Z Z converge at node Z, and nodes X and Y converge at node Z, and nodes X and Y contains (V =..), then V = contains (V =..), then V = ФФ(V, .., V) has (V, .., V) has been inserted at Z.been inserted at Z.

2.2. Each mention of V has been replaced by Each mention of V has been replaced by a mention of Va mention of Vii

3.3. V and the corresponding VV and the corresponding Vii have the have the

same value.same value.

Page 8: Stanford University CS243 Winter 2006 Wei Li 1 SSA

8CS243 Winter 2006Stanford University

OverviewOverview

SSA RepresentationSSA Representation SSA ConstructionSSA Construction

Step 1: Place Step 1: Place ФФ statements statements Step 2: Rename all variablesStep 2: Rename all variables

Converting out of SSAConverting out of SSA

Page 9: Stanford University CS243 Winter 2006 Wei Li 1 SSA

9CS243 Winter 2006Stanford University

ФФ Placement Placement

a = …

a = Ф(a ,a)

a = Ф(a ,a)

Place minimal

number of ФФ

functions

Page 10: Stanford University CS243 Winter 2006 Wei Li 1 SSA

10CS243 Winter 2006Stanford University

RenamingRenaming

a1=

a1+5

a4 =

a4+5

a2=

a3= Ф(a1,a2)

a3+5

Page 11: Stanford University CS243 Winter 2006 Wei Li 1 SSA

11CS243 Winter 2006Stanford University

SSA Construction (I)SSA Construction (I)

Step 1: Place Step 1: Place ФФ statements by statements by computing iterated dominance frontiercomputing iterated dominance frontier

Page 12: Stanford University CS243 Winter 2006 Wei Li 1 SSA

12CS243 Winter 2006Stanford University

CFGCFG

A control flow graph G = (V, E)A control flow graph G = (V, E) Set V contains distinguished nodes Set V contains distinguished nodes

START and ENDSTART and END every node is reachable from STARTevery node is reachable from START END is reachable from every node in G. END is reachable from every node in G. START has no predecessorsSTART has no predecessors END has no successors.END has no successors.

Predecessor, successor, pathPredecessor, successor, path

Page 13: Stanford University CS243 Winter 2006 Wei Li 1 SSA

13CS243 Winter 2006Stanford University

Dominator RelationDominator Relation

If X appears on every path from START to If X appears on every path from START to Y, then X Y, then X dominatesdominates Y. Y.

Domination is both reflexive and transitive.Domination is both reflexive and transitive. idom(Y): immediate dominator of Yidom(Y): immediate dominator of Y Dominator TreeDominator Tree

START is the rootSTART is the root Any node Y other than START has idom(Y) as Any node Y other than START has idom(Y) as

its parentits parent Parent, child, ancestor, descendantParent, child, ancestor, descendant

Page 14: Stanford University CS243 Winter 2006 Wei Li 1 SSA

14CS243 Winter 2006Stanford University

Dominator Tree ExampleDominator Tree Example

START

a

b c

d

END

START

CFG DT

Page 15: Stanford University CS243 Winter 2006 Wei Li 1 SSA

15CS243 Winter 2006Stanford University

Dominator Tree ExampleDominator Tree Example

START

a

b c

d

END

START

a

CFG DT

Page 16: Stanford University CS243 Winter 2006 Wei Li 1 SSA

16CS243 Winter 2006Stanford University

Dominator Tree ExampleDominator Tree Example

START

a

b c

d

END

START

a

b c

CFG DT

Page 17: Stanford University CS243 Winter 2006 Wei Li 1 SSA

17CS243 Winter 2006Stanford University

Dominator Tree ExampleDominator Tree Example

START

a

b c

d

END

START

a

db c

CFG DT

Page 18: Stanford University CS243 Winter 2006 Wei Li 1 SSA

18CS243 Winter 2006Stanford University

Dominator Tree ExampleDominator Tree Example

START

a

b c

d

END

START

a

d

END

b c

CFG DT

Page 19: Stanford University CS243 Winter 2006 Wei Li 1 SSA

19CS243 Winter 2006Stanford University

Dominance FrontierDominance Frontier

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

Page 20: Stanford University CS243 Winter 2006 Wei Li 1 SSA

20CS243 Winter 2006Stanford University

DF ExampleDF Example

START

a

b c

d

END

START

a

d

END

b c

CFG

DT

DF(c) = ?

DF(a) = ?

Page 21: Stanford University CS243 Winter 2006 Wei Li 1 SSA

21CS243 Winter 2006Stanford University

DF ExampleDF Example

START

a

b c

d

END

START

a

d

END

b c

CFG

DT

DF(c) = {d}

DF(a) = ?

Page 22: Stanford University CS243 Winter 2006 Wei Li 1 SSA

22CS243 Winter 2006Stanford University

DF ExampleDF Example

START

a

b c

d

END

START

a

d

END

b c

CFG

DT

DF(c) = {d}

DF(a) = {END}

Page 23: Stanford University CS243 Winter 2006 Wei Li 1 SSA

23CS243 Winter 2006Stanford University

Computing DFComputing DF

DF(X) is the union of the following setsDF(X) is the union of the following sets DFDFlocallocal(X), a set of successor nodes that X (X), a set of successor nodes that X

doesn’t strictly dominatedoesn’t strictly dominate E.g. DFE.g. DFlocallocal(c) = {d} (c) = {d}

DFDFupup(Z) for all Z (Z) for all Z єє Children(X)Children(X) DFDFupup(Z) = {Y (Z) = {Y єє DF(Z) | idom(Z) doesn’t strictly DF(Z) | idom(Z) doesn’t strictly

dominate Y}dominate Y} E.g. X = a, Z = d, Y = ENDE.g. X = a, Z = d, Y = END

Page 24: Stanford University CS243 Winter 2006 Wei Li 1 SSA

24CS243 Winter 2006Stanford University

Iterated Dominance FrontierIterated Dominance Frontier

DF(SET) is the union of DF(X), where X DF(SET) is the union of DF(X), where X єє SET.SET.

Iterated dominance frontier DFIterated dominance frontier DF++(SET) is (SET) is the limit ofthe limit of DFDF11 = DF(SET) and DF = DF(SET) and DFi+1i+1 = DF(SET U DF = DF(SET U DFii))

Page 25: Stanford University CS243 Winter 2006 Wei Li 1 SSA

25CS243 Winter 2006Stanford University

Computing JoinsComputing Joins

J(SET) of join nodesJ(SET) of join nodes Set of all nodes ZSet of all nodes Z There are two nonnull CFG paths that start at There are two nonnull CFG paths that start at

two distinct nodes in SET and converge at Z.two distinct nodes in SET and converge at Z. Iterated join JIterated join J++(SET) is the limit of (SET) is the limit of

JJ11 = J(SET) and J = J(SET) and Ji+1i+1 = J(SET U J = J(SET U Jii))

JJ++(SET) = DF(SET) = DF++(SET) (SET)

Page 26: Stanford University CS243 Winter 2006 Wei Li 1 SSA

26CS243 Winter 2006Stanford University

Placing Placing Ф Functions Functions

For each variable VFor each variable V Add all nodes with assignments to V to Add all nodes with assignments to V to

worklist Wworklist W While X in WWhile X in W do do

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

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

Page 27: Stanford University CS243 Winter 2006 Wei Li 1 SSA

27CS243 Winter 2006Stanford University

Computational ComplexityComputational Complexity

Constructing SSA takes Constructing SSA takes OO(A(Atottot * avrgDF), where * avrgDF), where AAtottot: total number of : total number of

assignmentsassignments avrgDF: weighted average avrgDF: weighted average

DF sizeDF size The computational The computational

complexity is O(ncomplexity is O(n22). ). e.g. nested repeat-until e.g. nested repeat-until

loops loops

S

a

b

c

E

d

Page 28: Stanford University CS243 Winter 2006 Wei Li 1 SSA

28CS243 Winter 2006Stanford University

Ф Placement Example Placement Example

a = …

a = Ф(a ,a)

a = Ф(a ,a)

Place Ф at Iterative

Dominance Frontiers

Page 29: Stanford University CS243 Winter 2006 Wei Li 1 SSA

29CS243 Winter 2006Stanford University

SSA Construction (II)SSA Construction (II)

Step 2: Rename all variables in original Step 2: Rename all variables in original program and program and ФФ functions, using functions, using dominator tree and rename stack to keep dominator tree and rename stack to keep track of the current names.track of the current names.

Page 30: Stanford University CS243 Winter 2006 Wei Li 1 SSA

30CS243 Winter 2006Stanford University

Variable RenamingVariable Renaming

Rename from the START node recursivelyRename from the START node recursively For node XFor node X

For each assignment (V = …) in XFor each assignment (V = …) in X Rename any use of V with the TOS of rename stackRename any use of V with the TOS of rename stack Push the new name VPush the new name Vii on rename stack on rename stack i = i + 1i = i + 1

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

dominator tree dominator tree For each assignment (V = …) in X For each assignment (V = …) in X

Pop VPop Vii in X from the rename stack in X from the rename stack

Page 31: Stanford University CS243 Winter 2006 Wei Li 1 SSA

31CS243 Winter 2006Stanford University

Renaming ExampleRenaming Example

a1=

a1+5

a =

a+5

a=

a= Ф(a1,a)

a+5

TOS

Rename expr

Page 32: Stanford University CS243 Winter 2006 Wei Li 1 SSA

32CS243 Winter 2006Stanford University

OverviewOverview

SSA RepresentationSSA Representation SSA ConstructionSSA Construction Converting out of SSAConverting out of SSA

Page 33: Stanford University CS243 Winter 2006 Wei Li 1 SSA

33CS243 Winter 2006Stanford University

Converting Out of SSAConverting Out of SSA

Mapping all VMapping all Vii to V? to V?

Page 34: Stanford University CS243 Winter 2006 Wei Li 1 SSA

34CS243 Winter 2006Stanford University

Overlapping Live RangesOverlapping Live Ranges Simply mapping all VSimply mapping all Vii to V may not work to V may not work

a1= b1

a1+5

b2=

a1+5

a1= b1

b1+5

b2=

b1+5

a1= b1

b1+5

b2=

a1+5

Page 35: Stanford University CS243 Winter 2006 Wei Li 1 SSA

35CS243 Winter 2006Stanford University

Converting Out of SSAConverting Out of SSA

Option 1: coloringOption 1: coloring Compute live ranges, and assign a unique Compute live ranges, and assign a unique

variable name for each live range variable name for each live range Similar techniques used in register allocation Similar techniques used in register allocation

to be covered next week.to be covered next week. Option 2: simply remove all Option 2: simply remove all ФФ functions functions

Every optimization in SSA needs to guarantee Every optimization in SSA needs to guarantee not to generate overlapping live ranges not to generate overlapping live ranges

Page 36: Stanford University CS243 Winter 2006 Wei Li 1 SSA

36CS243 Winter 2006Stanford University

ReferenceReference

““Efficient Computing Static Single Efficient Computing Static Single Assignment Form and the Control Assignment Form and the Control Dependence Graph”, R. Cytron, J. Dependence Graph”, R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and F. Ferrante, B. Rosen, M. Wegman, and F. K. Zadeck, Transactions on K. Zadeck, Transactions on Programming Languages and Systems Programming Languages and Systems (TOPLAS), Oct 1991. (TOPLAS), Oct 1991. http://citeseer.ist.psu.edu/cytron91efficientlhttp://citeseer.ist.psu.edu/cytron91efficiently.htmly.html

Page 37: Stanford University CS243 Winter 2006 Wei Li 1 SSA

37CS243 Winter 2006Stanford University

BackupBackup

Page 38: Stanford University CS243 Winter 2006 Wei Li 1 SSA

38CS243 Winter 2006Stanford University

Handling ArraysHandling Arrays

Difficult to treat A[i] as a variableDifficult to treat A[i] as a variable

The entire array can be treated like a The entire array can be treated like a scalar.scalar.

= 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 39: Stanford University CS243 Winter 2006 Wei Li 1 SSA

39CS243 Winter 2006Stanford University

Unnecessary Liveness Unnecessary Liveness

W operator may introduce unnecessary W operator may introduce unnecessary liveness for A. Introduce HW (HiddenW). 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