Top Banner
CMPUT 680 - Compiler Des ign and Optimization 1 CMPUT680 - Winter 2006 Topic G: Static Single- Assignment Form José Nelson Amaral http://www.cs.ualberta.ca/~amaral/courses/680
78

CMPUT680 - Winter 2006

Jan 21, 2016

Download

Documents

karsen

CMPUT680 - Winter 2006. Topic G: Static Single-Assignment Form José Nelson Amaral http://www.cs.ualberta.ca/~amaral/courses/680. Reading Material. Chapter 19 of the “Tiger book” (with a grain of salt!!). Bilardi, G., Pingali, K., “The Static Single Assignment Form and its - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

1

CMPUT680 - Winter 2006

Topic G: Static Single-Assignment Form

José Nelson Amaralhttp://www.cs.ualberta.ca/~amaral/courses/680

Page 2: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

2

Reading Material

Chapter 19 of the “Tiger book” (with a grain of salt!!).

Bilardi, G., Pingali, K., “The Static Single Assignment Form and its Computation,” unpublished? (citeseer).Cytron, R., Ferrante, J., Rosen, B. K., Wegman, M. N., Zadeck, F. K., “An Efficient Method of Computing Static Single Assignment Form,” ACM Symposium on Principles of Programming Languages (PoPL), pp. 25-35, Austin, TX, Jan., 1989.Cytron, R., Ferrante, J., Rosen, B. K., Wegman, M. N., “Efficiently Computing Static Single Assignment Form and the Control Dependence Graph,” ACM Transactions on Programming Languages and Systems (TOPLAS), Vol. 13, No. 4, October, 1991, pp. 451-490.Sreedhar, V. C., Gao, G. R., “A Linear Time Algorithm for Placing -Nodes,” ACM Symposium on Principles of Programming Languages (PoPL), pp. 62-73, 1995.

Page 3: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

3

Static Single-Assignment Form

Each variable has only one definition in the program text.

This single static definition can be in a loop andmay be executed many times. Thus even in aprogram expressed in SSA, a variable can be

dynamically defined many times.

Page 4: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

4

Advantages of SSA

Simpler dataflow analysisNo need to use use-def/def-use chains, which requires NM space for N uses and M definitions

SSA form relates in a useful way with dominance structures. SSA simplifies algorithms that construct interference graphs.

Page 5: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

5

SSA Form in Control-Flow Path Merges

b M[x]a 0

if b<4

a b

c a + b

B1

B2

B3

B4

Is this code in SSA form?

No, two definitions of a appear inthe code (in B1 and B3)

How can we transform this codeinto a code in SSA form?

We can create two versions ofa, one for B1 and another for B3.

Page 6: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

6

SSA Form in Control-Flow Path Merges

b M[x]a1 0

if b<4

a2 b

c a? + b

B1

B2

B3

B4

But which version should weuse in B4 now?

We define a fictional function that

“knows” which control path was

taken to reach the basic block B4:

( )⎩⎨⎧

=B3 from B4at arrive weif a2

B2 from B4at arrive weif a1a2a1,φ

Page 7: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

7

SSA Form in Control-Flow Path Merges

b M[x]a1 0

if b<4

a2 b

a3 (a2,a1) c a3 + b

B1

B2

B3

B4

But which version should weuse in B4 now?

We define a fictional function that

“knows” which control path was

taken to reach the basic block B4:

( )⎩⎨⎧

=B3 from B4at arrive weif a2

B2 from B4at arrive weif a1a2a1,φ

Page 8: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

8

A Loop Examplea 0

b a+1c c+ba b*2if a < N

return

a1 0b0 undefc0 undef

a3 (a1,a2)b1 (b0,b2)c2 (c0,c1)b2 a3+1c1 c2+b2a2 b2*2if a < N

return

(b0,b2) is not necessary because b1 is never used. But the phase that generates

functions does not know it. Unnecessary functions are later

eliminated by dead code elimination.

Page 9: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

9

The Function

How can we implement a function that “knows”which control path was taken?

Answer 1: We don’t!! The function is used onlyto connect use to definitions during

optimization, but is never implemented.

Answer 2: If we must execute the function, we can implement it by inserting MOVE instructions

in all control paths.

Page 10: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

10

Criteria for Inserting Functions

We could insert one function for each variableat every join point (a point in the CFG with morethan one predecessor). But that would be wasteful.

What criteria should we use to insert a function for a variable a at node z of the CFG?

Intuitively, we should add a function if there are two definitions of a that can reach

the point z through distinct paths.

Page 11: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

11

Path Convergence Criterion

(Cytron-Ferrante/89)

Insert a function for a variable a at a node z ifall the following conditions are true:1. There is a block x that defines a2. There is a block y x that defines a3. There is a non-empty paths xz and yz4. Paths xz and yz don’t have any nodes in common other than z5. The node z does not appear within both xz and yz prior to the end, but it may appear in one or the other.

Note: The start node contains an implicit definition of every variable.

Page 12: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

12

-Candidates are Join Nodes

Notice that according to the path convergencecriterion, the node z that will receive the functionmust be a join node.

z is the first node that joins the paths Pxz and Pyz.

Page 13: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

13

Iterated Path-Convergence Criterion

The function itself is a definition of a. Therefore the path-convergence criterion

is a set of equations that must be satisfied.

while there are nodes x, y, z satisfying conditions 1-5 and z does not contain a function for ado insert a (a0, a1, …, an) at node z

This algorithm is extremely costly, because itrequires the examination of every triple of

nodes x, y, z and every path from x to z and from y to z.

Can we do better?

Page 14: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

14

The SSA Conversion Problem

For each variable x defined in a CFG G=(V,E), given the set of nodes S V that contain a definition for x, find the minimal set, J(S) of nodes that requires a (xi,xj) function.

By definition, the START node defines allthe variables, therefore S V, START S.

If we need to compute nodes for severalvariables, it may be efficient to precomputedata structures based on the CFG.

Page 15: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

15

Processing Time for SSA Conversion

The performance of an SSA conversion algorithmshould be measured by the processing time Tp,the preprocessing space Sp, and the query time Tq.

(Shapiro and Saint 1970): outline an algorithm

(Reif and Tarjan 1981): extend the Lengauer-Tarjan dominator algorithm to compute -nodes.

(Cytron et al. 1991): show that SSA conversion can use the idea of dominance frontiers, resulting on an O(|V|2) algorithm.

(Sreedhar and Gao, 1995): An O(|E|) algorithm, but in private commun. with Pingali in 1996 admits that it is in practice 5 times slower than Cytron et al.

Page 16: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

16

Processing Time for SSA Conversion

Bilardi, Pingali, 1999: present a generalized framework and a parameterized Augmented Dominator Tree (ADT) algorithm that allows for a space-time tradeoff. They show that Cytron et al. and Gao-Shreedhar are special cases of the ADT algorithm.

Bilardi and Pingali describe three strategies to compute-placement:

•Two-Phase Algorithms•Lock-Step Algorithms•Lazy Algorithms

Page 17: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

17

Two-Phase Algorithms

First build the entire Dominance Frontier Graph, then find the nodes reachable from S

SimpleDF Graph may be quite large

DF Computation

Reachability

DF Graph

J(S)

S

CFG

Page 18: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

18

Lock-Step Algorithms

Performs the reachability computation incrementallywhile the DF relation is computed.

DF ComputationReachability

J(S)

• Avoid storing the DF Graph.• Perform computations at all nodes of the graph, even though most are irrelevant• Inneficient when computing the -nodes for many variables.

CFG

S

Page 19: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

19

Lazy Algorithms

Lazily compute only the portion fo the DF Graph thatis needed. Carefully select a portion of the DF Graph to compute eagerly (before it is needed).

A Two-Phase Algorithmis an extreme case of a lazy algorithm.

DF Computation

Reachability

DF GraphSubGraph

J(S)

S

CFG

Page 20: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

20

Computing a Dominator Tree

(Lowry and Medlock, 1969): Introduce the problem and give an O(n4) algorithm.

(Lengauer and Tarjan, 1979): Give a complicated O(m(m.n)) algorithm [(m.n) is the inverse Ackermann’s function].

(Harel, 1985): Give a linear time algorithm.

(Alstrup, Harel and Thorup, 1997): Give a simpler version ofHarel’s algorithm.

(n: # of nodes; m: # of edges)

Page 21: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

21

Dominance Property of the SSA Form

In SSA form definitions dominate uses, i.e.:1. If x is used in a function in block n, then the definition of x dominates every predecessor of n.2. If x is used in a non- statement in block n, then the definition of x dominates n.

Page 22: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

22

The Dominance Frontier

A node x dominates a node w if every path from the startnode to w must go through x.

A node x strictly dominates a node w if x dominatesw and x w.

The dominance frontier of a node x is the set ofall nodes w such that x dominates a predecessor

of w, but x does not strictly dominates w.

Page 23: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

23

Example

1

13

2

3

412

10 11

9

8

6 7

5

What is the dominance frontier of node 5?

Page 24: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

24

Example

1

13

2

3

412

10 11

9

8

6 7

5

First we must find all nodes that node 5 dominates.

Page 25: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

25

Example

A node w is in the dominance frontier of node 5 if 5 dominates a predecessor of w, but 5 does not strictlydominates w itself. What is the dominance frontier of 5?

1

13

2

3

412

10 11

9

8

6 7

5

Page 26: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

26

Example

1

13

2

3

412

10 11

9

8

6 7

5

A node w is in the dominance frontier of node 5 if 5 dominates a predecessor of w, but 5 does not strictlydominates w itself. What is the dominance frontier of 5?

Page 27: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

27

Example

1

13

2

3

412

10 11

9

8

6 7

5

DF(5) = {4, 5, 12, 13}

A node w is in the dominance frontier of node 5 if 5 dominates a predecessor of w, but 5 does not strictlydominates w itself. What is the dominance frontier of 5?

Page 28: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

28

Dominance Frontier Criterion

Dominance Frontier Criterion:If a node x contains a definition of variable a,then any node z in the dominance frontier ofx needs a function for a.

Can you think of an intuitive explanation for why a node in the dominance frontier of another node must be a join node?

Page 29: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

29

Example

1

13

2

3

412

10 11

9

8

6 7

5

If a node (12) is in the dominance frontier of

another node (5), than there must beat least two paths converging to (12).

These paths must be non-intersecting, andone of them (5,7,12)

must contain a node strictlydominated by (5).

Page 30: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

30

Dominator Tree

To compute the dominance frontiers, we first computethe dominator tree of the CFG.

There is an edge from node x to node y in the dominator tree if node x immediately dominates node y.

I.e., x dominates yx, and x does notdominate any other dominator of y.

Dominator trees can be computed using the Lengauer-Tarjan algorithm(1979).

See sec. 19.2 of Appel.

Page 31: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

31

Example: Dominator Tree

1

13

2

3

412

10 11

9

8

6 7

5

1

3

6 7

10 11

4 5 12 92 13

8Control Flow Graph

Dominator Tree

Page 32: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

32

Local Dominance Frontier

Cytron-Ferrante define the local dominance frontier of a node n as:

DFlocal[n] = successors of n in the CFG that are not strictly dominated by n

Page 33: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

33

Example: Local Dominance Frontier

1

13

2

3

412

10 11

9

8

6 7

5

Control Flow Graph

In the example, what arethe local dominance

frontier of nodes 5, 6 and 7?

DFlocal[5] = DFlocal[6] = {4,8}DFlocal[7] = {8,12}

Page 34: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

34

Dominance Frontier Inherited From Its

Children

The dominance frontier of a node n is formed by itslocal dominance frontier plus nodes that are passed up by the children of n in the dominator tree.

The contribution of a node c to its parents dominance frontier is defined as [Cytron-Ferrante, 1991]:

DFup[c] = nodes in the dominance frontier of c that are not strictly dominated by the immediate dominator of c

Page 35: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

35

Example: Local Dominance Frontier

1

13

2

3

412

10 11

9

8

6 7

5

Control Flow Graph

In the example, what arethe contributions of nodes6, 7, and 8 to its parent

dominance frontier?

First we computethe DF and the immediatedominator of each node:DF[6] = {4,8}, idom(6)= 5DF[7] = {8,12}, idom(7)= 5DF[8] = {5,13}, idom(8)= 5

Page 36: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

36

Example: Local Dominance Frontier

1

13

2

3

412

10 11

9

8

6 7

5

Control Flow Graph

First we computethe DF and the immediatedominator of each node:DF[6] = {4,8}, idom(6)= 5DF[7] = {8,12}, idom(7)= 5DF[8] = {5,13}, idom(8)= 5

Now we check for the DFup condition: DFup[6] = {4}DFup[7] = {12}DFup[8] = {5,13}

Page 37: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

37

A note on implementation

We want to represent these sets efficiently:DF[6] = {4,8} DF[7] = {8,12} DF[8] = {5,13}

If we use bitvectors to represent these sets:DF[6] = 0000 0001 0001 0000DF[7] = 0001 0001 0000 0000DF[8] = 0010 0000 0010 0000

Page 38: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

38

Strictly Dominated Sets

1

3

6 7

10 11

4 5 12 92 13

8

Dominator Tree

We can also represent the strictly dominated sets as vectors:SD[1] = 0011 1111 1111 1100SD[2] = 0000 0000 0000 1000SD[5] = 0000 0001 1100 0000SD[9] = 0000 1100 0000 0000

Page 39: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

39

A note on implementation

DFup[c] = nodes in the dominance frontier of c that are not strictly dominated by the immediate dominator of c

If we use bitvectors to represent these sets:DF[6] = 0000 0001 0001 0000DF[7] = 0001 0001 0000 0000DF[8] = 0010 0000 0010 0000

SD[5] = 0000 0001 1100 0000

DFup[c] = DF[6] ^ ~SD[5]

Page 40: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

40

Dominance Frontier Inherited From Its

Children

The dominance frontier of a node n is formed by itslocal dominance frontier plus nodes that are passed up by the children of n in the dominator tree. Thus the dominance frontier of a node n is defined as [Cytron-Ferrante, 1991]:

[ ] [ ][ ]

[ ]cDFnDFnDF upnDTchildrenc

local ∈∪∪=

Page 41: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

41

Example: Local Dominance Frontier

1

13

2

3

412

10 11

9

8

6 7

5

Control Flow Graph

What is DF[5]?Remember that:

DFlocal[5] = DFup[6] = {4}DFup[7] = {12}DFup[8] = {5,13}DTchildren[5] = {6,7,8}

Page 42: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

42

Example: Local Dominance Frontier

1

13

2

3

412

10 11

9

8

6 7

5

Control Flow Graph

What is DF[5]?Remember that:

DFlocal[5] = DFup[6] = {4}DFup[7] = {12}DFup[8] = {5,13}DTchildren[5] = {6,7,8}

Thus, DF[5] = {4, 5, 12, 13}

Page 43: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

43

Join Sets

In order to insert -nodes for a variable x that is defined in a set of nodes S={n1, n2, …, nk} we needto compute the iterated set of join nodes of S.

Given a set of nodes S of a control flow graph G, the set of join nodes of S, J(S), is defined as follows:

J(S) ={z G| two paths Pxz and Pyz in G that have z as its first common node, x S and y S}

Page 44: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

44

Iterated Join Sets

Because a -node is itself a definition of a variable,once we insert -nodes in the join set of S, we needto find out the join set of S J(S). Thus, Cytron-Ferrante define the iterated join set of a set of nodes S, J+(S), as the limit of the sequence:

( )( )ii JSJJ

SJJ

∪=

=

+1

1

Page 45: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

45

Iterated Dominance Frontier

We can extend the concept of dominance frontierto define the dominance frontier of a set of nodes as:

)()( XDFSDFSX∈

= U

Now we can define the iterated dominance frontier,DF+(S), of a set of nodes S as the limit of the sequence:

( )ii DFSDFDF

SDFDF

∪=

=

+1

1 )(

Exercise:

Find an example in which

the IDF of a set S is different

from the DF of the set!

Exercise:

Find an example in which

the IDF of a set S is different

from the DF of the set!

Page 46: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

46

Location of -Nodes

Given a variable x that is defined in a set of nodes S={n1, n2, …, nk} the set of nodes that must receive-nodes for x is J+(S).

An important result proved by Cytron-Ferrante is that:

( ) ( )SDFSJ ++ =

Thus we are mostly interested in computing the iterated dominance frontier of a set of nodes.

Page 47: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

47

Algorithms to Compute Dominance Frontier

The algorithm to insert -nodes, due to Cytron andFerrante (1991), computes the dominance frontierof each node in the set S before computing theiterated dominance frontier of the set.

In 1994, Shreedar and Gao proposed a simple,linear algorithm for the insertion of -nodes.

In the worst case, the combination of the dominancefrontier of the sets can be quadratic in the number of nodes in the CFG. Thus, Cytron-Ferrante’salgorithm has a complexity O(N2).

Page 48: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

48

Sreedhar and Gao’s DJ Graph

1

13

2

3

412

10 11

9

8

6 7

5

1

3

6 7

10 11

4 5 12 92 13

8Control Flow Graph

Dominator Tree

Page 49: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

49

Sreedhar and Gao’s DJ Graph

1

13

2

3

412

10 11

9

8

6 7

5

1

3

6 7

10 11

4 5 12 92 13

8Control Flow Graph

Dominator Tree

D nodes

Page 50: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

50

Sreedhar and Gao’s DJ Graph

1

13

2

3

412

10 11

9

8

6 7

5

1

3

6 7

10 11

4 5 12 92 13

8Control Flow Graph

Dominator Tree

D nodes

J nodes

Page 51: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

51

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

What is the DF[5]?

Page 52: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

52

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

SubTree(5) = {5, 6, 7, 8}

Initialization: DF[5] =

Page 53: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

53

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

SubTree(5) = {5, 6, 7, 8}

There are three edgesoriginating in 5: {56, 57, 58}

but they are all D-edges

Initialization: DF[5] =

Page 54: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

54

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

SubTree(5) = {5, 6, 7, 8}

There are two edgesoriginating in 6:

{64, 68}but 8.level > 5.level

Initialization: DF[5] = After visiting 6: DF = {4}

Page 55: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

55

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

SubTree(5) = {5, 6, 7, 8}

There are two edgesoriginating in 7:

{78, 712}again 8.level > 5.level

Initialization: DF[5] = After visiting 6: DF = {4}

After visiting 7: DF = {4,12}

Page 56: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

56

Shreedar-Gao’s Dominance Frontier Algorithm

DominanceFrontier(x)0: DF[x] = 1: foreach y SubTree(x) do2: if((y z == J-edge) and3: (z.level x.level))4: then DF[x] = DF[x] z

1

3

6 7

10 11

4 5 12 92 13

8

SubTree(5) = {5, 6, 7, 8}

There are two edgesoriginating in 8:

{85, 813}both satisfy cond. in steps 2-3

Initialization: DF[5] = After visiting 6: DF = {4}

After visiting 7: DF = {4,12}After visiting 8: DF = {4, 12, 5, 13}

Page 57: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

57

Shreedhar-Gao’s -Node Insertion Algorithm

Using the D-J graph, Shreedhar and Gao proposea linear time algorithm to compute the iterateddominance frontier of a set of nodes.

An important intuition in Shreedhar-Gao’s algorithm is:

If two nodes x and y are in S, and y is an ancestor of x in the dominator tree, then if we compute DF[x] first, we do not need to recompute DF[x] when computing DF[y].

Page 58: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

58

Shreedhar-Gao’s -Node Insertion Algorithm

Shreedhar-Gao’s algorithm also use a work listof nodes hashed by their level in the dominatortree and a visited flag to avoid visiting thesame node more than once.

The basic operation of the algorithm is similar totheir dominance-frontier algorithm, but it requiresa careful implementation to deliver the linear-timecomplexity.

Page 59: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

59

Dead-Code Elimination in SSA Form

Because there is only one definition for eachvariable, if the list of uses of the variable

is empty, the definition is dead.

When a statement v x y is eliminated becausev is dead, this statement must be removed fromthe list of uses of x and y. Which might cause

those definitions to become dead.

Thus we need to iterate the dead code elimination algorithm.

Page 60: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

60

Simple Constant Propagation in SSA

If there is a statement v c, where c is a constant,then all uses of v can be replaced for c.

A function of the form v (c1, c2, …, cn) where allci 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

In the next slide we assume that x, y, z are variablesand a, b, c are constants.

Page 61: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

61

Linear Time Optimizations in SSA

formCopy propagation: The statement x (y) or the statementx y can be deleted and y can substitute every use of x.

Constant folding: If we have the statement x a b, we can evaluate c a b at compile time and replace the statement for x c

Constant conditions: The conditional

if a < b goto L1 else L2

can be replaced for goto L1 or goto L2, according to thecompile time evaluation of a < b, and the CFG, use lists,adjust accordingly

Unreachable Code: eliminate unreachable blocks.

Page 62: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

62

Single Assignment Form

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 63: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

63

Single Assignment Form

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 64: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

64

Single Assignment Form

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 65: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

65

Single Assignment Form

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 66: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

66

Single Assignment Form

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 67: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

67

Single Assignment Form

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 68: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

68

Example:Constant Propagation

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

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

Page 69: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

69

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 70: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

70

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 71: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

71

Example:Is this the end?

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 ofdead code.

See algorithm on pg. 454-455 of Appel.

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 72: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

72

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

B5 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 73: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

73

Example: Single Argument -Function

Elimination

k3 k2+1

return j2

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

j4 (1)k4 (k3)

B2

B5

B7

B4

k3 k2+1

return j2

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

j4 1k4 k3

B2

B5

B7

B4

Page 74: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

74

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 75: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

75

Example: Dead Code Elimination

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 76: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

76

Example: -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 77: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

77

Example: 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 78: CMPUT680 - Winter 2006

CMPUT 680 - Compiler Design and Optimization

78

Example: Dead Code Elimination

k3 k2+1

return 1

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

B2

B5

B4 return 1 B4