53
1 Region-Based Data Flow Analysis

1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

Embed Size (px)

Citation preview

Page 1: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

1

Region-Based Data Flow Analysis

Page 2: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

2

LoopsLoops

Loops in programs deserve special treatment Because programs spend most of their time

executing loops, improving the performance of loops can have a significant impact

If a program does not contain any loops, we can obtain the answers to data-flow problems by making just one pass through the program

Page 3: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

3

DominatorsDominators

A node n1 dominates a node n2 , written n1 d

om n2, if every path from the ENTRY node of

the flow graph to n2 goes through n1

Every node dominates itself

Page 4: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

4

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10

10 dom {10} 9 dom {9} 8 dom {8,9,10} 7 dom {7,8,9,10} 6 dom {6} 5 dom {5} 4 dom {4,5,6,7,8,9,10} 3 dom {3,4,5,6,7,8,9,10} 2 dom {2} 1 dom {1,2,3,4,5,6,7,8,9,10}

Page 5: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

5

Dominator TreesDominator Trees

The dom relation between the nodes of a flow graph can be represented as a tree, called the dominator tree

The ENTRY node of the graph is the root of the tree, and each node n dominates only its descendants in the tree

Each node n has a unique immediate dominator m that is the last dominator of n on any path from the ENTRY node to n

Page 6: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

6

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10

1

2 3

4

5 6 7

8

9 10

Page 7: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

7

Finding DominatorsFinding Dominators

If p1, p2, …, pk are all the predecessors of n, a

nd n d, then d dom n iff d dom pi for each i

Page 8: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

8

Finding DominatorsFinding Dominators

Direction Forwards

Transfer fB(x) = x { B }

function

Boundary OUT[ENTRY] = { ENTRY }

Meet() Equations OUT[B] = fB(IN[B])

IN[B] = P, pred(B) OUT[P]

Initialization OUT[B] = N (set of nodes)

Page 9: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

9

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10

D(1) = {1}D(2) = {1,2}D(3) = {1,3}D(4) = {1,3,4}D(5) = {1,3,4,5}D(6) = {1,3,4,6}D(7) = {1,3,4,7}D(8) = {1,3,4,7,8}D(9) = {1,3,4,7,8,9}D(10) = {1,3,4,7,8,10}

Page 10: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

10

Depth-First OrderingDepth-First Ordering

A depth-first traversal of a graph starts at the ENTRY node and visits nodes as far away from the ENTRY node as quickly as possible

The route of a depth-first traversal forms a tree called a depth-first spanning tree (DEST) of the graph

A depth-first ordering of the nodes in a graph is the reverse of the postorder traversal

Page 11: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

11

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10910

1

3 2

4

6 5

7

8

10, 9, 8, 7, 6, 5, 4, 3, 2, 1

Page 12: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

12

Depth-First Traversal Depth-First Traversal AlgorithmAlgorithmvoid search(n) { mark n “visited”; for (each successor s of n) if (s is “unvisited”) { add edge n s to T; search(s); } dfn[n] = c; c = c – 1;}

main( ) { T = ; for (each node n of G) mark n “unvisited”; c = number of nodes of G; search(n0);}

Page 13: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

13

Edges in a Depth-First Edges in a Depth-First Spanning TreeSpanning Tree An advancing edge goes from a node m to a

descendant of m in the tree A retreating edge goes from a node m to an

ancestor of m in the tree A cross edge is an edge m n such that

neither m nor n is an ancestor of the other in the tree

Page 14: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

14

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10910

1

3 2

4

6 5

7

8

Page 15: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

15

Back EdgesBack Edges

A back edge is an edge a b whose head b dominates its tail a

For any flow graph, every back edge is retreating, but not every retreating edge is a back edge

Page 16: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

16

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10910

1

3 2

4

6 5

7

8

Page 17: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

17

ReducibilityReducibility

A flow graph is said to be reducible if all its retreating edges in any DFST are also back edges

If a flow graph is nonreducible, all the back edges are retreating edges in any DFST, but each DFST may have additional retreating edges that are not back edges.

A flow graph is reducible if after removing all the back edges, the remaining graph is acyclic

Page 18: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

18

A Reducible ExampleA Reducible Example

1

2

3

4

5 6

7

8

9 10910

1

3 2

4

6 5

7

8

Page 19: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

19

A Nonreducible ExampleA Nonreducible Example

1

2 3

Page 20: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

20

Depth of a Flow GraphDepth of a Flow Graph

Given a DFST for a flow graph, the depth of the flow is the largest number of retreating edges on any cycle-free path

Page 21: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

21

An ExampleAn Example

910

1

3 2

4

6 5

7

8

10 7 4 3

depth is 3

Page 22: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

22

Natural LoopsNatural Loops

A natural loop is defined by two properties: It must have a single-entry node, called the

header. This entry node dominates all nodes in the loop

There must be a back edge that enters the loop header

Given a back edge n d, we define the natural loop of the edge to be d plus the set of nodes that can reach n without going through d. Node d is the header of the loop.

Page 23: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

23

Constructing Natural Constructing Natural LoopsLoops

void insert(m) { if (m is not in loop) { loop = loop {m}; push m onto S; }}

main(n d) { S = empty; loop = {d}; insert(n); while (S is not empty) { pop m off S; for (each predecessor p of m) insert(p); }}

Page 24: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

24

An ExampleAn Example

1

2

3

4

5 6

7

8

9 10

107:{7,8,10} 74 :{4,5,6,7,8,10} 43 :{3,4,5,6,7,8,10} 83 :{3,4,5,6,7,8,10} 91 :{1,2,3,4,5,6,7,8,9,10}

Page 25: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

25

Innermost LoopsInnermost Loops

Unless two loops have the same header, they are either disjoint or one is nested within the other

When two loops have the same header, but neither is nested within the other, they are combined and treated as a single loop

An innermost loop is one that contains no other loops

Page 26: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

26

An ExampleAn Example

B0

B1

B2 B3

Page 27: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

27

Speed of Convergence of Speed of Convergence of Iterative Data-Flow Iterative Data-Flow AlgorithmsAlgorithms The maximum number of iterations the

algorithm may take is the product of the height of the lattice and the number of nodes in the flow graph

It is possible to order the evaluation such that the algorithm converges in a much smaller number of iterations

Page 28: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

28

Speed of Convergence of Speed of Convergence of Iterative Data-Flow Iterative Data-Flow AlgorithmsAlgorithms If all useful information propagates along

acyclic paths, we have an opportunity to tailor the order in which we visit nodes in iterative data-flow algorithms, so that after relatively few passes through the nodes we can be sure information has passed along all the acyclic paths

Page 29: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

29

Information PropagationInformation Propagation

In reaching definitions, if a definition d is in IN[B], then there is some acyclic path from the block containing d to B such that d is in the IN’s and OUT’s all along that path

In constant propagation, information may propagate through cyclic paths L: a = b b = c c = 1 goto L

Page 30: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

30

Using Depth-First OrderUsing Depth-First Order

Process the basic blocks in the flow graph in depth-first order

The number of iterations needed is no more than two greater than the depth

35193516234541017

Page 31: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

31

Region-Based AnalysisRegion-Based Analysis

In the iterative analysis, we create transfer functions for basic blocks, then find the fixedpoint solution of data-flow values by repeated passes over the blocks

A region-based analysis finds transfer functions that summarize the execution of progressively larger regions of the program

Ultimately, transfer functions for entire procedures are constructed and then applied to get the desired data-flow values directly

Page 32: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

32

RegionsRegions

A region of a flow graph is a collection of nodes N and edges E such that

There is a header h in N that dominates all the nodes in N

If some node m can reach a node n in N without going through h, then m is also in N

E is the set of all the control flow edges between nodes n1 and n2 in N, except (possibly) for some that enter h

Page 33: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

33

An ExampleAn Example

B1

B2

B3

B4

N: {B1, B2}, E: {B1B2}.

N: {B1, B2, B3}, E: {B1B2, B2B3, B1B3}.

N: {B2, B3}, E: {B2B3}.

Page 34: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

34

Region HierarchiesRegion Hierarchies

First, every block is a region by itself. These regions are called leaf regions

Then we order the natural loops from the inside out. We replace each loop by a node in two steps:

The body of the loop is replaced by a node. This node is called a body region

The body region and the loop back edge are replaced by a node. This node is called a loop region

Page 35: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

35

An ExampleAn Example

d1: i = m - 1d2: j = nd3: a = u1

d4: i = i + 1

d5: a = u2

B1

B2

B3

B4d6: j = u3

B5

Page 36: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

36

An ExampleAn Example

R1

R2

R4

R5

R3

R1

R6

R5

R1

R7

R5

R8

Page 37: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

37

Overview of Region-Based Overview of Region-Based AnalysisAnalysis This is a two-pass analysis. The first pass co

mputes transfer functions bottom-up. The second pass computes data-flow values top-down

We first consider the bottom-up pass For each region R, and for each subregion R’

within R, we compute a transfer function fR,IN[R’] that summarizes the effect of executing all possible paths leading from the entry of R to the entry of R’, while staying within R

Page 38: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

38

Overview of Region-Based Overview of Region-Based AnalysisAnalysis We say that a block B within R is an exit bloc

k of region R if it has an outgoing edge to some block outside R

We also compute a transfer function for each exit block B of R, denoted fR,OUT[B], that summarizes the effect of executing all possible paths within R, leading from the entry of R to the exit of B

Page 39: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

39

Overview of Region-Based Overview of Region-Based AnalysisAnalysis We begin with regions that are single blocks,

where fB,IN[B] is just the identity function I and fB,

OUT[B] is the transfer function fB for the block B itself

We then proceed up the region hierarchy, computing transfer functions for progressively larger regions

Eventually, we reach the top of the hierarchy and compute the transfer functions for the top region Rn that is the entire flow graph

Page 40: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

40

Overview of Region-Based Overview of Region-Based AnalysisAnalysis If R is a body region, then the edges belongin

g to R form an acyclic graph on the subregions of R. for (each subregion S immediately contained in R, in topological order) {

fR,IN[S] = Bpred(S) fR,OUT[B]; for (each exit block B in S) fR,OUT[B] = fS,OUT[B] 。 fR,IN[S]; }

Page 41: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

41

Overview of Region-Based Overview of Region-Based AnalysisAnalysis If R is a loop region, then we only need to acc

ount for the effect of the back edges to the header of R. let S be the body region immediately contained in R;

fR,IN[S] = ( Bpred(S) fS,OUT[B])*; for (each exit block B in R) fR,OUT[B] = fS,OUT[B] 。 fR,IN[S];

Page 42: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

42

Overview of Region-Based Overview of Region-Based AnalysisAnalysis We now consider the top-down pass For each region, we compute the data-flow v

alues at the entry For region Rn, IN[Rn] = IN[ENTRY]

For each subregion R in Rn, IN[R] = fRn,IN[R](IN[Rn])

We repeat until we reach the basic block at the leaves of the region hierarchy

Page 43: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

43

Necessary Assumptions Necessary Assumptions about Transfer Functionsabout Transfer Functions For region-based analysis to work, we need t

hree primitive operations on transfer functions: composition, meet, and closure

Let f1 and f2 be transfer functions of nodes n1 and n2. The effect of executing n1 followed by n2 is represented by f2 。 f1 f2 。 f1(x) = gen2 ((gen1 (x – kill1)) – kill2) = (gen2 (gen1 – kill2)) (x – (kill1 kill2))

Page 44: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

44

Necessary Assumptions Necessary Assumptions about Transfer Functionsabout Transfer Functions The meet of two transfer functions f1 and f2, f1

f f2 , is defined by (f1 f f2)(x) = f1(x) f2(x)

(f1 f f2)(x) = f1(x) f2(x) = (gen1 (x – kill1)) (gen2 (x – kill2)) = (gen1 gen2) (x – (kill1 kill2))

Page 45: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

45

Necessary Assumptions Necessary Assumptions about Transfer Functionsabout Transfer Functions If f represents the transfer function of a cycle.

Assume that the cycle may be executed 0 or more times. The closure of f is defined by

f* = n0 f n or f* = I (n>0 f n)

f 2(x) = f (f (x)) = gen ((gen (x – kill)) – kill) = gen (x – kill) = f 1(x) f*(x) = I f 1(x) f 2(x) … = x (gen (x – kill)) = gen x

Page 46: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

46

An ExampleAn Example

d1: i = m - 1d2: j = nd3: a = u1

d4: i = i + 1

d5: a = u2

B1

B2

B3

B4d6: j = u3

B5

genB , killB:B1 :{d1,d2,d3}, {d4,d5,d6}.B2 :{d4}, {d1}.B3 :{d5}, {d3}.B4 :{d6}, {d2}.B5 : , .

Page 47: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

47

An ExampleAn Example

R1

R2

R4

R5

R3

For Ri, 1 i 5,

fRi,IN[Bi](x) = I,

fRi,OUT[Bi](x) = genBi

(x – killBi)

Page 48: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

48

An ExampleAn Example

R1

R2

R4

R5

R3

R6

Transfer Function gen kill

fR6,IN[R2] = I

fR6,OUT[B2] = fR2,OUT[B2] 。 fR6,IN[R2] {d4} {d1}

fR6,IN[R3] = fR6,OUT[B2] {d4} {d1}

fR6,OUT[B3] = fR3,OUT[B3] 。 fR6,IN[R3] {d4,d5} {d1,d3}

fR6,IN[R4] = fR6,OUT[B2] fR6,OUT[B3] {d4,d5} {d1}

fR6,OUT[B4] = fR4,OUT[B4] 。 fR6,IN[R4] {d4,d5,d6} {d1,d2}

Page 49: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

49

An ExampleAn Example

R1

R6

R5

R7

Transfer Function gen kill

fR7,IN[R6] = f*R6,OUT[B4] {d4,d5,d6}

fR7,OUT[B3] = fR6,OUT[B3] 。 fR7,IN[R6] {d4,d5,d6} {d1,d3}

fR7,OUT[B4] = fR6,OUT[B4] 。 fR7,IN[R6] {d4,d5,d6} {d1,d2}

Page 50: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

50

An ExampleAn Example

R1

R7

R5

R8

Transfer Function gen kill

fR8,IN[R1] = I

fR8,OUT[B1] = fR1,OUT[B1] {d1,d2,d3} {d4,d5,d6}

fR8,IN[R7] = fR8,OUT[B1] {d1,d2,d3} {d4,d5,d6}

fR8,OUT[B3] = fR7,OUT[B3] 。 fR8,IN[R7] {d2,d4,d5,d6} {d1,d3}

fR8,OUT[B4] = fR7,OUT[B4] 。 fR8,IN[R7] {d3,d4,d5,d6} {d1,d2}

fR8,IN[R5] = fR8,OUT[B3] fR8,OUT[B4] {d2,d3,d4,d5,d6} {d1}

fR8,OUT[B5] = fR5,OUT[B5] 。 fR8,IN[R5] {d2,d3,d4,d5,d6} {d1}

Page 51: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

51

An ExampleAn Example

IN[R8] =

IN[R1] =fR8,IN[R1](IN[R8]) =

IN[R7] =fR8,IN[R7](IN[R8]) = {d1,d2,d3}

IN[R5] =fR8,IN[R5](IN[R8]) = {d2,d3,d4,d5,d6}

IN[R6] =fR7,IN[R6](IN[R7]) = {d1,d2,d3,d4,d5,d6}

IN[R4] =fR6,IN[R4](IN[R6]) = {d2,d3,d4,d5,d6}

IN[R3] =fR6,IN[R3](IN[R6]) = {d2,d3,d4,d5,d6}

IN[R2] =fR6,IN[R2](IN[R6]) = {d1,d2,d3,d4,d5,d6}

Page 52: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

52

Handling Nonreducible GraHandling Nonreducible Graphsphs A node n has k predecessors can be splitted

by replacing n by k nodes n1, n2, …, nk. The ith predecessor of n becomes the predecessor of ni only, while all successors of n become successors of all of the ni’s

1

2 3

1

2a 3

2b

Page 53: 1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving

53

Handling Nonreducible GraHandling Nonreducible Graphsphs When we split a node, the transfer function

s for the nodes in the region must be duplicated

When we compute the in’s, the in’s is computed by taking the meet of the in’s of all its representatives. We compute the out’s similarly