advanced compiler techniques

94
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Data Flow Analysis

Upload: cruz

Post on 19-Mar-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Advanced Compiler Techniques. Data Flow Analysis. LIU Xianhua School of EECS, Peking University. REVIEW. Introduc tion to optimization Control Flow Analysis Basic knowledge Basic blocks Control-flow graphs Local Optimizations Peephole optimizations. Outline. Some Basic Ideas - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced Compiler Techniques

Advanced Compiler Techniques

LIU Xianhua

School of EECS, Peking University

Data Flow Analysis

Page 2: Advanced Compiler Techniques

“Advanced Compiler Techniques”

REVIEW

• Introduction to optimization

• Control Flow Analysis

• Basic knowledge– Basic blocks– Control-flow graphs

• Local Optimizations– Peephole optimizations

2

Page 3: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Outline

• Some Basic Ideas• Reaching Definitions• Available Expressions• Live Variables

3

Page 4: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Levels of Optimizations

• Local– inside a basic block

• Global (intraprocedural)– Across basic blocks– Whole procedure analysis

• Interprocedural– Across procedures– Whole program analysis

4

Page 5: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Dataflow Analysis

• Last lecture: – How to analyze and transform within a

basic block

• This lecture: – How to do it for the entire procedure

5

Page 6: Advanced Compiler Techniques

6

An Obvious Theorem

boolean x = true;while (x) { . . . // no change to x}• Doesn’t terminate.• Proof: only assignment to xis at top, so x is always true.

x = true

if x == true

“body”

“Advanced Compiler Techniques”

Page 7: Advanced Compiler Techniques

7

Formulation: Reaching Definitions Each place some

variable x is assigned is a definition.

Ask: for this use of x, where could x last have been defined.

In our example: only at x=true.

d1: x = true

if x == true

d2: a = 10

d2

d1

d1d2

d1

“Advanced Compiler Techniques”

Page 8: Advanced Compiler Techniques

8

Clincher

• Since at x == true, d1 is the only definition of x that reaches, it must be that x is true at that point.

• The conditional is not really a conditional and can be replaced by a jump.

“Advanced Compiler Techniques”

Page 9: Advanced Compiler Techniques

9

Not Always That Easy

int i = 2;int j = 3;while (i != j) { if (i < j) i += 2; else j += 2;}

“Advanced Compiler Techniques”

Page 10: Advanced Compiler Techniques

10

Not Always That Easy Build the Flow Graph

d1: i = 2d2: j = 3

if i != j

if i < j

d4: j = j+2d3: i = i+2

d1, d2, d3, d4

d1

d3 d4

d2

d2, d3, d4

d1, d3, d4d1, d2, d3, d4d1, d2, d3, d4

“Advanced Compiler Techniques”

Page 11: Advanced Compiler Techniques

11

DFA Is Sufficient Only In this example, i can be defined in two

places, and j in two places. No obvious way to discover that i!=j is

always true. But OK, because reaching definitions is

sufficient to catch most opportunities for constant folding (replacement of a variable by its only possible value).

“Advanced Compiler Techniques”

Page 12: Advanced Compiler Techniques

12

Be Conservative!

• (Code optimization only)• It’s OK to discover a subset of the

opportunities to make some code-improving transformation.

• It’s not OK to think you have an opportunity that you don’t really have.

“Advanced Compiler Techniques”

Page 13: Advanced Compiler Techniques

13

Example: Be Conservative

boolean x = true;while (x) { . . . *p = false; . . .}• Is it possible thatp points to x?

d1: x = true

if x == true

d2: *p = false

d1

d2

Anotherdef of x

“Advanced Compiler Techniques”

Page 14: Advanced Compiler Techniques

14

Possible Resolution

• Just as data-flow analysis of “reaching definitions” can tell what definitions of x might reach a point, another DFA can eliminate cases where p definitely does not point to x.

• Example: the only definition of p is p = &y andthere is no possibility that y is an alias of x. “Advanced Compiler Techniques”

Page 15: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Data-Flow Analysis Schema

• Data-flow value: at every program point

• Domain: The set of possible data-flow values for this application

• IN[S] and OUT[S]: the data-flow values before and after each statement s

• Data-flow problem: find a solution to a set of constraints on the IN [s] ‘s and OUT[s] ‘s, for all statements s. – based on the semantics of the

statements ("transfer functions" ) – based on the flow of control. 15

Page 16: Advanced Compiler Techniques

16

Data-Flow Equations (1)

• A statement/basic block can generate a definition.

• A statement/basic block can either1. Kill a definition of x if it surely

redefines x.2. Transmit a definition if it may not

redefine the same variable(s) as that definition.

“Advanced Compiler Techniques”

Page 17: Advanced Compiler Techniques

17

Data-Flow Equations (2)

• Variables:1. IN[s] = set of data flow values the

before a statement s.2. OUT[s] = set of data flow values the

after a statement s.• Extends:

1. IN[B] = set of definitions reaching the beginning of block B.

2. OUT[B] = set of definitions reaching the end of B.

“Advanced Compiler Techniques”

Page 18: Advanced Compiler Techniques

18

Data-Flow Equations (3)

• Two kinds of Constraints:Transfer Functions:OUT[s] = fs(IN[s])IN[s] = fs(OUT[s]) reversed~

Control Flow Constraints:If B consists of statements s1,s2,…,sn IN[si+1] = OUT[si]

“Advanced Compiler Techniques”

Page 19: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Between Blocks

• Forward analysis(eg: Reaching definitions)– Transfer equations – OUT[B] = fB(IN[B])Where fB = fsn ◦ ••• ◦ fs2 ◦ fs1

– Confluence equations – IN[B] = UP a predecessor of B OUT[P]

• Backward analysis(eg: live variables)– Transfer equations – IN[B] = fB (OUT[B])– Confluence equations – OUT[B] = US a successor of B IN[S].

19

Page 20: Advanced Compiler Techniques

20

Confluence Equations IN(B) = ∪predecessor P of B OUT(P) OUT(B) = ∪successor S of B IN(S)

P2

B

P1

{d1, d2, d3}

{d2, d3}{d1, d2}

“Advanced Compiler Techniques”

Page 21: Advanced Compiler Techniques

21

Transfer Equations

• OUT[B] = fB(IN[B])• IN[B] = fB(OUT[B]) ~reverse

• Generate a definition in the block if its variable is not definitely rewritten later in the basic block.

• Kill a definition if its variable is definitely rewritten in the block.

“Advanced Compiler Techniques”

Page 22: Advanced Compiler Techniques

22

Example: Gen and Kill An internal definition may be both killed

and generated. For any block B:

OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B)

d1: y = 3 d2: x = y+zd3: *p = 10d4: y = 5

IN = {d2(x), d3(y), d3(z), d5(y), d6(y), d7(z)}

Kill includes {d2(x),d3(y), d5(y), d6(y),…}

Gen = {d2(x), d3(x), d3(z),…, d4(y)}

OUT = {d2(x), d3(x), d3(z),…, d4(y), d7(z)} “Advanced Compiler Techniques”

Page 23: Advanced Compiler Techniques

23

Iterative Solution to Equations For an n-block flow graph, there are 2n

equations in 2n unknowns. Alas, the solution is not unique.

Standard theory assumes a field of constants; sets are not a field.

Use iterative solution to get the least fixed-point. Identifies any def that might reach a

point.

“Advanced Compiler Techniques”

Page 24: Advanced Compiler Techniques

24

Example: Reaching Definitions

d1: x = 5

if x == 10

d2: x = 15

B1

B3

B2

IN(B1) = {}

OUT(B1) = {

OUT(B2) = {

OUT(B3) = {

d1}

IN(B2) = { d1,

d1,

IN(B3) = { d1,

d2}

d2}

d2}

d2}

“Advanced Compiler Techniques”

Page 25: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Outline

• Some Basic Ideas• Reaching Definitions• Available Expressions• Live Variables

25

Page 26: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Reaching Definitions

• Concept of definition and use– a = x+y is a definition of a is a use of x and y

• A definition reaches a use if value written by definitionmay be read by use

26

Page 27: Advanced Compiler Techniques

27

Reaching Definitions

“Advanced Compiler Techniques”

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

Page 28: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Reaching Def. and Const. Propagation

• Is a use of a variable a constant?– Check all reaching definitions– If all assign variable to same constant– Then use is in fact a constant

• Can replace variable with constant

28

Page 29: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is a Constant in s = s+a*b?

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

Yes!On all reaching definitionsa = 4

Page 30: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Constant Propagation Transform

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + 4*b;i = i + 1; return s

Yes!On all reaching definitionsa = 4

Page 31: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is b Constant in s = s+a*b?

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

No!One reaching definition withb = 1 One reaching definition withb = 2

Page 32: Advanced Compiler Techniques

“Advanced Compiler Techniques”

SplittingPreserves Information Lost At Merges

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

i < n

s = s + a*b;i = i + 1; return s

Page 33: Advanced Compiler Techniques

“Advanced Compiler Techniques”

SplittingPreserves Information Lost At Merges

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*b;i = i + 1; return s

s = 0; a = 4; i = 0;k == 0

b = 1; b = 2;

i < n

s = s + a*1;i = i + 1;

return s

i < n

s = s + a*2;i = i + 1;

return s

Page 34: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Computing Reaching Definitions

• Compute with sets of definitions– represent sets using bit vectors– each definition has a position in bit

vector• At each basic block, compute

– definitions that reach start of block– definitions that reach end of block

• Do computation by simulating execution of program until reach fixed point 34

Page 35: Advanced Compiler Techniques

“Advanced Compiler Techniques”

1: s = 0; 2: a = 4; 3: i = 0;k == 0

4: b = 1; 5: b = 2;

0000000

11100001110000

1111100

1111100 1111100

1111111

1111111 1111111

1 2 3 4 5 6 7

1 2 3 4 5 6 7 1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1110000

1111000 1110100

1111100

0101111 1111100

1111111i < n

1111111return s6: s = s + a*b;

7: i = i + 1;

Page 36: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Formalizing Reaching Definitions

• Each basic block has– IN - set of definitions that reach beginning of

block– OUT - set of definitions that reach end of block– GEN - set of definitions generated in block– KILL - set of definitions killed in block

• GEN[s = s + a*b; i = i + 1;] = 0000011• KILL[s = s + a*b; i = i + 1;] = 1010000• Compiler scans each basic block to derive

GEN and KILL sets36

Page 37: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Dataflow Equations

• IN[b] = OUT[b1] U ... U OUT[bn]– where b1, ..., bn are predecessors of b in CFG

• OUT[b] = (IN[b] - KILL[b]) U GEN[b]• IN[entry] = 0000000• Result: system of equations

• KILLB= KILL1 U KILL2 U… U KILLn

• GENB=GENn U ( GENn-1-KILLn ) U ( GENn-2-KILLn-1-KILLn) U

… U ( GEN1-KILL2-KILL3-…-KILLn )37

Page 38: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Solving Equations• Use fixed point algorithm• Initialize with solution of OUT[b] = 0000000• Repeatedly apply equations

– IN[b] = OUT[b1] U ... U OUT[bn]– OUT[b] = (IN[b] - KILL[b]) U GEN[b]

• Until reach fixed point • Until equation application has no further

effect• Use a worklist to track which equation

applications may have a further effect

38

Page 39: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Reaching Definitions Algorithmfor all nodes n in N

OUT[n] = emptyset; // OUT[n] = GEN[n];IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; // N = all nodes in graph

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = emptyset; for all nodes p in predecessors(n)

IN[n] = IN[n] U OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all nodes s in successors(n)

Changed = Changed U { s };

39

Page 40: Advanced Compiler Techniques

40

Iterative Solution

IN(entry) = ∅;for each block B do OUT(B)= ∅;while (changes occur) do for each block B do { IN(B) = ∪predecessors P of B OUT(P); OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B); }

“Advanced Compiler Techniques”

Page 41: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Example

41

Page 42: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Outline

• Some Basic Ideas• Reaching Definitions• Available Expressions• Live Variables

42

Page 43: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Available Expressions

• An expression x+y is available at a point p if – every path from the initial node to p must

evaluate x+y before reaching p, – and there are no assignments to x or y after

the evaluation but before p.• Available Expression information can be

used to do global (across basic blocks) CSE• If expression is available at use, no need

to reevaluate it43

Page 44: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Example: Available Expression

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

44

Page 45: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

YES!

45

Page 46: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

YES!

46

Page 47: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

NO!

47

Page 48: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

NO!

48

Page 49: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

NO!

49

Page 50: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

YES!

50

Page 51: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Is the Expression Available?

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

YES!

51

Page 52: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

52

Page 53: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

53

Page 54: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = a + c

j = a + b + c + d

b = a + dh = c + f

54

Page 55: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = f

j = a + b + c + d

b = a + dh = c + f

55

Page 56: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = f

j = a + b + c + d

b = a + dh = c + f

56

Page 57: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = f

j = a + c + b + d

b = a + dh = c + f

57

Page 58: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = f

j = f + b + d

b = a + dh = c + f

58

Page 59: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Use of Available Expressions

a = b + cd = e + ff = a + c

g = f

j = f + b + d

b = a + dh = c + f

59

Page 60: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Computing Available Expressions• Represent sets of expressions using bit

vectors• Each expression corresponds to a bit• Run dataflow algorithm similar to reaching

definitions• Big difference

– definition reaches a basic block if it comes from ANY predecessor in CFG

– expression is available at a basic block only if it is available from ALL predecessors in CFG

60

Page 61: Advanced Compiler Techniques

61

Gen(B) and Kill(B)

• An expression x+y is generated if it is computed in B, and afterwards there is no possibility that either x or y is redefined.

• An expression x+y is killed if it is not generated in B and either x or y is possibly redefined.

“Advanced Compiler Techniques”

Page 62: Advanced Compiler Techniques

62

Example

x = x+yz = a+b Generates

a+b

Kills x+y,w*x, etc.

Kills z-w,x+z, etc.

“Advanced Compiler Techniques”

Page 63: Advanced Compiler Techniques

63

Example Expression

s1: x+y2: i<n3: i+c4: x==0

a = x+y;x == 0

x = z;b = x+y;

i < n

c = x+y;i = i+c;

d = x+y

i = x+y;

0000

1001

1001

1000

1100 1100

1000

“Advanced Compiler Techniques”

Page 64: Advanced Compiler Techniques

64

Example : Global CSE Transform Expression

s1: x+y2: i<n3: i+c4: x==0

must use same tempfor CSE in all blocks

a = x+y;t = ax == 0

x = z;b = x+y;t = b

i < n

c = x+y;i = i+c;

d = x+y

i = x+y;

0000

1001

1000

1000

1100 1100

1001

“Advanced Compiler Techniques”

Page 65: Advanced Compiler Techniques

65

Example : Global CSE Transform Expression

s1: x+y2: i<n3: i+c4: x==0

must use same tempfor CSE in all blocks

a = x+y;t = ax == 0

x = z;b = x+y;t = b

i < n

c = t;i = i+c;

d = t

i = t;

0000

1001

1001

1000

1100 1100

1000

“Advanced Compiler Techniques”

Page 66: Advanced Compiler Techniques

66

Formalizing Analysis Each basic block has

IN - set of expressions available at start of block OUT - set of expressions available at end of block GEN - set of expressions computed in block (and

not killed later) KILL - set of expressions killed in in block (and

not re-computed later) GEN[x = z; b = x+y] = 1000 KILL[x = z; b = x+y] = 0001 Compiler scans each basic block to derive

GEN and KILL sets

“Advanced Compiler Techniques”

Page 67: Advanced Compiler Techniques

67

Dataflow Equations IN[b] = OUT[b1] ... OUT[bn]

where b1, ..., bn are predecessors of b in CFG OUT[b] = (IN[b] - KILL[b]) U GEN[b] IN[entry] = 0000 Result: system of equations

“Advanced Compiler Techniques”

Page 68: Advanced Compiler Techniques

68

Solving Equations Use fixed point algorithm IN[entry] = 0000 Initialize OUT[b] = 1111 Repeatedly apply equations

IN[b] = OUT[b1] ... OUT[bn] OUT[b] = (IN[b] - KILL[b]) U GEN[b]

Use a worklist algorithm to reach fixed point

“Advanced Compiler Techniques”

Page 69: Advanced Compiler Techniques

69

Available Expressions Algorithmfor all nodes n in N

OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; // N = all nodes in graph

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = E; // E is set of all expressions for all nodes p in predecessors(n)

IN[n] = IN[n] OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all nodes s in successors(n)

Changed = Changed U { s };“Advanced Compiler Techniques”

Page 70: Advanced Compiler Techniques

70

Transfer Equations

• Transfer is the same idea:

OUT(B) = (IN(B) – Kill(B)) ∪ Gen(B)

“Advanced Compiler Techniques”

Page 71: Advanced Compiler Techniques

71

Confluence Equations

• Confluence involves intersection, because an expression is available coming into a block if and only if it is available coming out of each predecessor.

IN(B) = ∩predecessors P of B OUT(P)

“Advanced Compiler Techniques”

Page 72: Advanced Compiler Techniques

72

Iterative Solution

IN(entry) = ∅;for each block B do OUT(B)= ALL;while (changes occur) do for each block B do { IN(B) = ∩predecessors P of B OUT(P); OUT(B) = (IN(B) – Kill(B)) ∪

Gen(B); }

“Advanced Compiler Techniques”

Page 73: Advanced Compiler Techniques

73

Why It Works

• An expression x+y is unavailable at point p iff there is a path from the entry to p that either:

1. Never evaluates x+y, or2. Kills x+y after its last evaluation.

• IN(entry) = ∅ takes care of (1).• OUT(B) = ALL, plus intersection

during iteration handles (2).

“Advanced Compiler Techniques”

Page 74: Advanced Compiler Techniques

74

Example

point p

Entry

x+ynevergen’d

x+y killed

x+ynevergen’d

“Advanced Compiler Techniques”

Page 75: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Duality In Two Algorithms

• Reaching definitions– Confluence operation is set union– OUT[b] initialized to empty set

• Available expressions– Confluence operation is set intersection– OUT[b] initialized to set of available

expressions• General framework for dataflow

algorithms.• Build parameterized dataflow analyzer

once, use for all dataflow problems75

Page 76: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Outline

• Some Basic Ideas• Reaching Definitions• Available Expressions• Live Variables

76

Page 77: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Live Variable Analysis

• A variable x is live at point p if – x is used along some path starting at p, and – no definition of x along the path before the

use.

• When is a variable x dead at point p?– No use of x on any path from p to exit node, or– If all paths from p redefine x before using x.

77

Page 78: Advanced Compiler Techniques

“Advanced Compiler Techniques”

What Use is Liveness Information?• Register allocation.

– If a variable is dead, can reassign its register– If x is not live on exit from a block, there is no

need to copy x from a register to memory.• Dead code elimination.

– Eliminate assignments to variables not read later.

– But must not eliminate last assignment to variable (such as instance variable) visible outside CFG.

– Can eliminate other dead assignments.– Handle by making all externally visible variables

live on exit from CFG78

Page 79: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Conceptual Idea of Analysis

• Simulate execution• But start from exit and go backwards in

CFG• Compute liveness information from end to

beginning of basic blocks

79

Page 80: Advanced Compiler Techniques

80

Liveness Example Assume a,b,c visible

outside method So are live on exit Assume x,y,z,t not

visible Represent Liveness

Using Bit Vector order is abcxyzt

a = x+y;t = a;c = a+x;x == 0

b = t+z;

c = y+1;

1100100

1110000

1100111

1000111

1100100

0101110

a b c x y z t

a b c x y z t

a b c x y z t

“Advanced Compiler Techniques”

Page 81: Advanced Compiler Techniques

81

Dead Code Elimination Assume a,b,c visible

outside method So are live on exit Assume x,y,z,t not

visible Represent Liveness

Using Bit Vector order is abcxyzt

a = x+y;t = a;c = a+x;x == 0

b = t+z;

c = y+1;

1100100

1110000

1100111

1000111

1100100

0101110

a b c x y z t

a b c x y z t

a b c x y z t

“Advanced Compiler Techniques”

Page 82: Advanced Compiler Techniques

82

Formalizing Analysis Each basic block has

IN - set of variables live at start of block OUT - set of variables live at end of block USE - set of variables with upwards exposed uses

in block (use prior to definition) DEF - set of variables defined in block prior to use

USE[x = z; x = x+1;] = { z } (x not in USE) DEF[x = z; x = x+1; y = 1;] = {x, y} Compiler scans each basic block to derive

USE and DEF sets

“Advanced Compiler Techniques”

Page 83: Advanced Compiler Techniques

83

Algorithmfor all nodes n in N - { Exit }

IN[n] = emptyset;OUT[Exit] = emptyset; IN[Exit] = use[Exit];Changed = N - { Exit };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

OUT[n] = emptyset; for all nodes s in successors(n)

OUT[n] = OUT[n] U IN[p];

IN[n] = use[n] U (out[n] - def[n]);

if (IN[n] changed) for all nodes p in predecessors(n) Changed = Changed U { p };

“Advanced Compiler Techniques”

Page 84: Advanced Compiler Techniques

84

Transfer Equations

• Transfer equations give IN’s in terms of OUT’s:

IN(B) = (OUT(B) – Def(B)) ∪ Use(B)

“Advanced Compiler Techniques”

Page 85: Advanced Compiler Techniques

85

Confluence Equations

• Confluence involves union over successors, so a variable is in OUT(B) if it is live on entry to any of B’s successors.

OUT(B) = ∪successors S of B IN(S)

“Advanced Compiler Techniques”

Page 86: Advanced Compiler Techniques

86

Iterative Solution

OUT(exit) = ∅;for each block B do IN(B)= ∅;while (changes occur) do for each block B do { OUT(B) = ∪successors S of B IN(S); IN(B) = (OUT(B) – Def(B)) ∪

Use(B); }

“Advanced Compiler Techniques”

Page 87: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Similar to Other Dataflow Algorithms

• Backward analysis, not forward• Still have transfer functions• Still have confluence operators• Can generalize framework to work

for both forwards and backwards analyses

87

Page 88: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Comparison

88

Page 89: Advanced Compiler Techniques

“Advanced Compiler Techniques”

ComparisonAvailable Expressionsfor all nodes n in N

OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = E; for all nodes p in predecessors(n)

IN[n] = IN[n] OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all

nodes s in successors(n) Changed =

Changed U { s };

Reaching Definitionsfor all nodes n in N

OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = emptyset; for all nodes p in predecessors(n)

IN[n] = IN[n] U OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all nodes s in

successors(n) Changed

= Changed U { s };

Live Variablesfor all nodes n in N - { Exit }

IN[n] = emptyset;OUT[Exit] = emptyset; IN[Exit] = use[Exit];Changed = N - { Exit };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

OUT[n] = emptyset; for all nodes s in successors(n)

OUT[n] = OUT[n] U IN[p];

IN[n] = use[n] U (out[n] - def[n]);

if (IN[n] changed) for all nodes p in

predecessors(n) Changed = Changed

U { p };

89

Page 90: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Comparison

Available Expressionsfor all nodes n in N

OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = E; for all nodes p in predecessors(n)

IN[n] = IN[n] OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all

nodes s in successors(n) Changed =

Changed U { s };

Reaching Definitionsfor all nodes n in N

OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = emptyset; for all nodes p in predecessors(n)

IN[n] = IN[n] U OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all nodes s in

successors(n) Changed

= Changed U { s }; 90

Page 91: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Comparison

Reaching Definitionsfor all nodes n in N

OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

IN[n] = emptyset; for all nodes p in predecessors(n)

IN[n] = IN[n] U OUT[p];

OUT[n] = GEN[n] U (IN[n] - KILL[n]);

if (OUT[n] changed) for all nodes s in

successors(n) Changed =

Changed U { s };

Live Variablefor all nodes n in N

IN[n] = emptyset;OUT[Exit] = emptyset; IN[Exit] = use[Exit];Changed = N - { Exit };

while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n };

OUT[n] = emptyset; for all nodes s in successors(n)

OUT[n] = OUT[n] U IN[p];

IN[n] = use[n] U (out[n] - def[n]);

if (IN[n] changed) for all nodes p in

predecessors(n) Changed =

Changed U { p };91

Page 92: Advanced Compiler Techniques

92

Pessimistic vs. Optimistic Analysis Available expressions is optimistic

(for common sub-expression elimination) Assume expressions are available at start of

analysis Analysis eliminates all that are not available Cannot stop analysis early and use current

result Live variables is pessimistic (for dead code

elimination) Assume all variables are live at start of

analysis Analysis finds variables that are dead Can stop analysis early and use current result

Dataflow setup same for both analyses Optimism/pessimism depends on intended use“Advanced Compiler Techniques”

Page 93: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Summary

• Dataflow Analysis– Control flow graph– IN[b], OUT[b], transfer functions, join points

• Paired analyses and transformations– Reaching definitions/constant propagation– Available expressions/common sub-expression

elimination– Live-variable analysis/Reg Alloc & Dead code

elimination

93

Page 94: Advanced Compiler Techniques

“Advanced Compiler Techniques”

Next Time

• Projects– CFG Construction, DFA & Optimization

DUE to 20th, Nov

• Data Flow Analysis: Foundation– DragonBook: §9.3