Compilerconstructie najaar 2019 - - PowerPoint PPT Presentation

compilerconstructie
SMART_READER_LITE
LIVE PREVIEW

Compilerconstructie najaar 2019 - - PowerPoint PPT Presentation

Compilerconstructie najaar 2019 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 10, vrijdag 6 december 2019 + werkcollege Code Optimization (2) 1


slide-1
SLIDE 1

Compilerconstructie

najaar 2019 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 10, vrijdag 6 december 2019 + ‘werkcollege’ Code Optimization (2)

1

slide-2
SLIDE 2

9.2 Introduction to Data-Flow Analysis

  • Optimizations depend on data-flow analysis, e.g.,

– Global common subexpression elimination – Dead-code elimination

  • Execution path yields program state at program point
  • Extract information from program state for data-flow analy-

sis

  • Usually infinite number of execution paths / program states
  • Different analyses extract different information

2

slide-3
SLIDE 3

Data-Flow Analysis (Examples)

  • Reaching definitions: which definitions (assignments of val-

ues) of variable x may reach program point? – Useful for debugging: May variable x be undefined? – Useful for constant folding: Can variable x only have one, constant value at program point?

3

slide-4
SLIDE 4

9.2.4 Computing Reaching Definitions

ENTRY

d1: i = m-1 d2: j = n d3: a = u1 B1

d4: i = i+1 d5: j = j-1 B2

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙

d6: a = u2 B3

❍❍❍❍❍❍❍❍❍ ❍ ❥ ❄

d7: i = u3 B4

✬ ✫ ✲ ❄

EXIT

Reaching definitions

  • Before B1: ∅
  • After B1: {d1, d2, d3}
  • Before B2: . . .
  • After B2: . . .

4

slide-5
SLIDE 5

9.2.2 The Data Flow Analysis Schema

Data flow values

  • IN[s]: before statement s
  • OUT[s]: after statement s
  • Transfer function fs

– forward: OUT[s] = fs(IN[s]) – backward: IN[s] = fs(OUT[s])

5

slide-6
SLIDE 6

Computing Reaching Definitions

  • Effect of single definition d : u = v op w:

– OUT[d] = {d} ∪ (IN[d] − . . .)

6

slide-7
SLIDE 7

Computing Reaching Definitions

Effect of single definition d : u = v op w:

  • OUT[d] = {d}∪(IN[d]−{all other definitions of u in program})
  • Hence,

fd(x) = {d} ∪ (x − {all other definitions of u in program}) = gend ∪ (x − killd) where gend = {d} killd = {all other definitions of u in program}

7

slide-8
SLIDE 8

Computing Reaching Definitions

Effect of block B, with definitions 1, 2, . . . , n: genB = {n, n − 1, . . . , 1} − { definitions killed afterwards } = genn ∪ (genn−1 − killn) ∪ (genn−2 − killn−1 − killn) . . . killB = kill1 ∪ kill2 ∪ . . . ∪ killn

8

slide-9
SLIDE 9

Computing Reaching Definitions

ENTRY

d1: i = m-1 d2: j = n d3: a = u1 B1

d4: i = i+1 d5: j = j-1 B2

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙

d6: a = u2 B3

❍❍❍❍❍❍❍❍❍ ❍ ❥ ❄

d7: i = u3 B4

✬ ✫ ✲ ❄

EXIT genB1 = {d1, d2, d3} killB1 = {d4, d5, d6, d7} genB2 = {d4, d5} killB2 = {d1, d2, d7} genB3 = {d6} killB3 = {d3} genB4 = {d7} killB4 = {d1, d4}

In this case, genB . . .

9

slide-10
SLIDE 10

Iterative Algorithm for Computing Reaching Definitions

OUT[ENTRY] = ∅ for each basic block B other than ENTRY OUT[B] = ∅ while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∪predecessors P of BOUT[P] OUT[B] = genB ∪ (IN[B] − killB)

}

Typical form of algorithm for forward data-flow analysis ∪ is meet operator (why ∪ ?) Example with B = B1, B2, B3, B4, EXIT. . .

10

slide-11
SLIDE 11

Implementation of Iterative Algorithm for Computing Reaching Definitions. . .

OUT[ENTRY] = ∅ for each basic block B other than ENTRY OUT[B] = ∅ while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∪predecessors P of BOUT[P] OUT[B] = genB ∪ (IN[B] − killB)

}

11

slide-12
SLIDE 12

Implementation of Iterative Algorithm for Computing Reaching Definitions

With bit vectors

Block B OUT[B]0 IN[B]1 OUT[B]1 IN[B]2 OUT[B]2 B1 000 0000 000 0000 111 0000 000 0000 111 0000 B2 000 0000 111 0000 001 1100 111 0111 001 1110 B3 000 0000 001 1100 000 1110 001 1110 000 1110 B4 000 0000 001 1110 001 0111 001 1110 001 0111 EXIT 000 0000 000 0000 001 0111 001 0111 001 0111

12

slide-13
SLIDE 13

Iterative Algorithm for Computing Reaching Definitions

OUT[ENTRY] = . . . for each basic block B other than ENTRY OUT[B] = . . . while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∪predecessors P of BOUT[P] OUT[B] = genB ∪ (IN[B] − killB)

}

Fixed point / steady state Often, solution depends on initialization

13

slide-14
SLIDE 14

9.2.5 Live-Variable Analysis

  • Variable x is live at program point p,

if value of x at p could be used later along some path

  • Otherwise x is dead at p
  • Information useful for register allocation (see lecture 7)
  • Information about later use must be propagated backwards

14

slide-15
SLIDE 15

Live-Variable Analysis

Effect of block B on live variables

  • useB:

variables that may be used in B prior to any definition in B (≈ gen)

  • def B:

variables that are (definitely) defined in B prior to any use

  • f that variable in B

(≈ kill)

15

slide-16
SLIDE 16

Computing Live Variables

ENTRY

d1: i = m-1 d2: j = n d3: a = u1 B1

d4: i = i+1 d5: j = j-1 B2

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙

d6: a = u2 B3

❍❍❍❍❍❍❍❍❍ ❍ ❥ ❄

d7: i = u3 B4

✬ ✫ ✲ ❄

EXIT useB1 = {m, n, u1} def B1 = {i, j, a} useB2 = {i, j} def B2 = ∅ useB3 = {u2} def B3 = {a} useB4 = {u3} def B4 = {i}

16

slide-17
SLIDE 17

Iterative Algorithm to Compute Live Variables

IN[EXIT] = ∅ for each basic block B other than EXIT IN[B] = ∅ while (changes to any IN occur) for each basic block B other than EXIT

{

OUT[B] = ∪successors S of BIN[S] IN[B] = useB ∪ (OUT[B] − def B)

}

Typical form of algorithm for backward data-flow analysis

17

slide-18
SLIDE 18

9.2.6 Available expressions

  • Is (value of) expression x op y available?
  • Useful for global common subexpression elimination
  • Can be decided with data-flow analysis

18

slide-19
SLIDE 19

Available Expressions (Example)

t1 = 4*i B1

t3 = 4*i B3

19

slide-20
SLIDE 20

Available Expressions (Example)

t1 = 4*i B1

t3 = 4*i B3

✟ ✟ ✟ ✟ ✟ ✙

B2

❍❍❍❍ ❍ ❥

?

20

slide-21
SLIDE 21

Available Expressions (Example)

t1 = 4*i B1

t3 = 4*i B3

✟ ✟ ✟ ✟ ✟ ✙

B2

❍❍❍❍ ❍ ❥

i = 17

21

slide-22
SLIDE 22

Available Expressions (Example)

t1 = 4*i B1

t3 = 4*i B3

✟ ✟ ✟ ✟ ✟ ✙

B2

❍❍❍❍ ❍ ❥

i = 17 t2 = 4*i

22

slide-23
SLIDE 23

Available Expressions (Example)

t1 = 4*i B1

t3 = 4*i B3

✟ ✟ ✟ ✟ ✟ ✙

B2

❍❍❍❍ ❍ ❥

i = 17 t1 = 4*i

23

slide-24
SLIDE 24

Computing Available Expressions

Effect of block B on available expressions

  • e genB:

expressions y op z that are definitely computed in B, and for which y and z are not subsequently redefined

  • e killB:

expressions y op z for which y and/or z are (or may be) defined in B, and that are not subsequently recomputed

24

slide-25
SLIDE 25

Computing e genB (Example)

S = ∅ For each statement x = y op z in block B (forwards)

  • add y op z to S
  • delete from S any expression involving x

Statement Available Expressions S ∅ a = b + c {b + c} b = a - d {a − d} c = b + c {a − d} d = a - d ∅

25

slide-26
SLIDE 26

Computing Available Expressions

OUT[ENTRY] = ∅ for each basic block B other than ENTRY OUT[B] = U while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∩predecessors P of BOUT[P] OUT[B] = e genB ∪ (IN[B] − e killB)

}

Why U . . .

26

slide-27
SLIDE 27

Available Expressions (Example)

ENTRY

a = b+c B1

d = a+e e = d+c B2

❄ ✬ ✫ ✲

d = b+c B3

EXIT

27

slide-28
SLIDE 28

Efficient Iterative Data-Flow Analysis

Example: computing reaching definitions

OUT[ENTRY] = ∅ for each basic block B other than ENTRY OUT[B] = ∅ while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∪predecessors P of BOUT[P] OUT[B] = genB ∪ (IN[B] − killB)

}

Order of blocks in second for-loop matters

28

slide-29
SLIDE 29

Efficient Iterative Data-Flow Analysis

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ❍❍❍❍ ❍ ❥ ❄ ✻ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❍❍❍❍ ❍ ❥ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❪

1 2 3 4 5 6 7 8 9 10

✬ ✫ ✲ ✬ ✫ ✲ ✩ ✪ ✛

Order of blocks in second for-loop matters

29

slide-30
SLIDE 30

Exercises

30

slide-31
SLIDE 31

Flow Graph For Data Flow Analysis

ENTRY

(1) a = 1 (2) b = 2 B1

(3) c = a+b (4) d = c-a B2

✏ ✏ ✏ ✏ ✏ ✏ ✏ ✮

(5) d = b+d B3

(6) d = a+b (7) e = e+1 B4

PPPPPP P q

(8) b = a+b (9) e = c-a B5

(10) a = b+d (11) b = a-d B6

EXIT

✬ ✫ ✲ ✩ ✪ ✛

31

slide-32
SLIDE 32

9.6 Loops in Flow Graphs

  • Optimizations of loops have significant impact
  • Loops affect speed of convergence of iterative DFA
  • Essential to identify loops
  • Used in region based analysis (not for exam)

32

slide-33
SLIDE 33

Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge

natural loop single entry

inside-out

  • ptimization

DFST

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ❄ PPPPPPPPPPPPPPPPP P q

retreating edge

depth DFST

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth DFST iterations DFA

33

slide-34
SLIDE 34

9.6.1 Dominators

  • Dominators:

– Node d dominates node n if every path from ENTRY node to n goes through d: d dom n – Node n dominates itself – Loop entry dominates all nodes in loop

  • Immediate dominator m of n:

last dominator on (any) path from ENTRY node to n – if d = n and d dom n, then d dom m

34

slide-35
SLIDE 35

Dominators (Example)

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ❍❍❍❍ ❍ ❥ ❄ ✻ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❍❍❍❍ ❍ ❥ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❪

1 2 3 4 5 6 7 8 9 10

✬ ✫ ✲ ✬ ✫ ✲ ✩ ✪ ✛

35

slide-36
SLIDE 36

Dominator Trees (Example)

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ❍❍❍❍ ❍ ❥ ❄ ✻ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❍❍❍❍ ❍ ❥ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❪

1 2 3 4 5 6 7 8 9 10

✬ ✫ ✲ ✬ ✫ ✲ ✩ ✪ ✛ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘

❅ ❅ ❅ ❅

❅ ❅ ❅ ❅

❅ ❅ ❅ ❅

1 2 3 4 5 6 7 8 9 10

36

slide-37
SLIDE 37

Finding Dominators

Forward data-flow analysis N is set of all nodes

OUT[ENTRY] = {ENTRY} for each node n other than ENTRY OUT[n] = N while (changes to any OUT occur) for each node n other than ENTRY

{

IN[n] = ∩predecessors m of nOUT[m] OUT[n] = IN[n] ∪ {n}

}

37

slide-38
SLIDE 38

Finding Dominators

Forward data-flow analysis Incorrect different initialization. . .

OUT[ENTRY] = {ENTRY} for each node n other than ENTRY OUT[n] = ∅ while (changes to any OUT occur) for each node n other than ENTRY

{

IN[n] = ∩predecessors m of nOUT[m] OUT[n] = IN[n] ∪ {n}

}

38

slide-39
SLIDE 39

Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge

natural loop single entry

inside-out

  • ptimization

DFST

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ❄ PPPPPPPPPPPPPPPPP P q

retreating edge

depth DFST

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth DFST iterations DFA

39

slide-40
SLIDE 40

9.6.6 Natural loops

  • Back edge a → b, if b dominates a
  • If loop has single-entry node, then compiler can assume cer-

tain initial conditions

  • Natural loop
  • 1. Has single-entry node: header
  • 2. Has back edge to header
  • Each back edge n → d determines natural loop, consisting of

– d – all nodes that can reach n without going through d

  • Constructing natural loop of back edge. . .

40

slide-41
SLIDE 41

Dominator Trees (Example)

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ❍❍❍❍ ❍ ❥ ❄ ✻ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❍❍❍❍ ❍ ❥ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❪

1 2 3 4 5 6 7 8 9 10

✬ ✫ ✲ ✬ ✫ ✲ ✩ ✪ ✛ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘

❅ ❅ ❅ ❅

❅ ❅ ❅ ❅

❅ ❅ ❅ ❅

1 2 3 4 5 6 7 8 9 10

41

slide-42
SLIDE 42

No Natural Loops

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘

❅ ❅ ❅ ❘ ✲ ✛

1 2 3

42

slide-43
SLIDE 43

Natural Loops

  • Useful property: unless two natural loops have same header

– either they are disjoint – or one is nested within other Allows for inside-out optimization

  • Assumption: if necessary, combine natural loops with same
  • header. . .

43

slide-44
SLIDE 44
  • Assumption: if necessary, combine natural loops with same
  • header. . .

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ♣ ♣ ❄

❅ ❅ ❅ ❅ ❘ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✕ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❆ ❑ ❏ ❏ ❏ ❫ ✡ ✡ ✡ ✢

1 2 3 4

44

slide-45
SLIDE 45

Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge

natural loop single entry

inside-out

  • ptimization

DFST

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ❄ PPPPPPPPPPPPPPPPP P q

retreating edge

depth DFST

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth DFST iterations DFA

45

slide-46
SLIDE 46

9.6.2 Depth-First Ordering

  • Depth-first traversal of graph

– Start from entry node – Recursively visit neighbours (in any order) – Hence, visit nodes far away from the entry node as quickly as it can (DF)

46

slide-47
SLIDE 47

A Depth-First Spanning Tree

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ❍❍❍❍ ❍ ❥ ❄ ✻ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❍❍❍❍ ❍ ❥ ✟ ✟ ✟ ✟ ✟ ✙ ❄ ✟ ✟ ✟ ✟ ✟ ✙ ❍❍❍❍ ❍ ❥ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❏ ❪

1 2 3 4 5 6 7 8 9 10

✬ ✫ ✲ ✬ ✫ ✲ ✩ ✪ ✛ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

1 2 3 4 5 6 7 8 9 10

✲ ✲ ✲ ❑ ✛

47

slide-48
SLIDE 48

9.6.3 Edges in Depth-First Spanning Tree

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

1 2 3 4 5 6 7 8 9 10

✲ ✲ ✲ ❑ ✛

  • Advancing edges
  • Retreating edges
  • Cross edges
  • Back edge a → b,

if b dominates a (regardless of DFST)

  • Each back edge is

retreating edge in DFST

  • Flow graph is reducible,

if each retreating edge in any DFST is back edge

48

slide-49
SLIDE 49

9.6.3 Edges in Depth-First Spanning Tree

A different depth-first spanning tree. . .

49

slide-50
SLIDE 50

9.6.3 Edges in Depth-First Spanning Tree

Two different depth-first spanning trees. . .

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘

❅ ❅ ❅ ❘ ✲ ✛

1 2 3

50

slide-51
SLIDE 51

9.6.4 Reducibility

  • In practice, almost every flow graph is reducible
  • Example of nonreducible flow graph

(with advancing edges)

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘

❅ ❅ ❅ ❘ ✲ ✛

1 2 3

  • To decide on reducibility:
  • 1. Remove back edges
  • 2. Is remaining graph acyclic?

51

slide-52
SLIDE 52

9.6.5 Depth of Flow Graph

  • Depth of DFST is largest number of retreating edges on any

cycle-free path

  • If flow graph is reducible, then depth is independent of DFST:

depth of flow graph

  • Depth ≤ depth of loop nesting in flow graph

52

slide-53
SLIDE 53

Depth of Flow Graph (Example)

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

1 2 3 4 5 6 7 8 9 10

✲ ✲ ✲ ❑ ✛

Depth is 3, because of path 10 → 7 → 4 → 3

53

slide-54
SLIDE 54

Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge

natural loop single entry

inside-out

  • ptimization

DFST

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ❄ PPPPPPPPPPPPPPPPP P q

retreating edge

depth DFST

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth DFST iterations DFA

54

slide-55
SLIDE 55

9.6.2 A Depth-First Ordering

✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ✚✙ ✛✘ ❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

❅ ❅ ❅ ❘

1 2 3 4 5 6 7 8 9 10

✲ ✲ ✲ ❑ ✛

  • Depth-First Ordering:

nodes in DFST in reverse of postorder

  • Example:

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

  • Edge m → n is

retreating, if and only if n comes before m in depth-first ordering

55

slide-56
SLIDE 56

9.6.7 Speed of Convergence

  • f Iterative Data-Flow Algorithms

In data-flow analysis, can significant events be propagated to node along acyclic path?

  • Yes for

– Reaching definitions – Live-variable analysis – Available expressions

  • No for

– Copy propagation If yes, then fast convergence possible

56

slide-57
SLIDE 57

Efficient Iterative Data-Flow Analysis

Example: computing reaching definitions

OUT[ENTRY] = ∅ for each basic block B other than ENTRY OUT[B] = ∅ while (changes to any OUT occur) for each basic block B other than ENTRY

{

IN[B] = ∪predecessors P of BOUT[P] OUT[B] = genB ∪ (IN[B] − killB)

}

Order of blocks in second for-loop matters

57

slide-58
SLIDE 58

Fast Convergence

  • Forward data-flow problem: visit nodes in depth-first-order
  • Recall:

edge m → n is retreating, if and only if n comes before m in depth-first ordering

  • Example: path of propagation of definition d:

3 → 5 → 19 → 35 → 16 → 23 → 45 → 4 → 10 → 17

  • Number of iterations: 1 + depth (+ 1)
  • Typical flow graphs have depth 2.75
  • Backward data-flow problem: visit nodes in reverse of depth-

first-order

58

slide-59
SLIDE 59

Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge

natural loop single entry

inside-out

  • ptimization

DFST

✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ❄ PPPPPPPPPPPPPPPPP P q

retreating edge

depth DFST

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth DFST iterations DFA

59

slide-60
SLIDE 60

Reducible Flow Graph G

domination

❆ ❆ ❆ ❆ ❆ ❆ ❯

back edge = retreating edge

natural loop single entry

inside-out

  • ptimization

DFST

PPPPPPPPPPPPPPPPP P q ❄

depth G

❆ ❆ ❆ ❆ ❆ ❆ ❯

DFO

  • rder of nodes

in iteration DFA

✁ ✁ ✁ ✁ ✁ ✁ ☛

1 + depth G iterations DFA

60

slide-61
SLIDE 61

En verder. . .

  • Vanmiddag, 16.15 uur: practicum over opdracht 4
  • Donderdag 12 december: inleveren opdracht 4
  • Donderdag 19 december, 14.15–17.15: tentamen
  • Vragenuur ?

61

slide-62
SLIDE 62

Compiler constructie

college 10 Code Optimization Chapters for reading: 9.2, 9.6

62