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
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
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
9.2.4 Computing Reaching Definitions Reaching definitions ENTRY • Before B 1 : ∅ ❄ d 1 : i = m-1 • After B 1 : { d 1 , d 2 , d 3 } d 2 : j = n B 1 • Before B 2 : . . . d 3 : a = u1 • After B 2 : . . . ✬ ✲ ❄ d 4 : i = i+1 B 2 d 5 : j = j-1 ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ✟ d 6 : a = u2 B 3 ❍❍❍❍❍❍❍❍❍ ❥ ❍ ❄ ✫ d 7 : i = u3 B 4 ❄ EXIT 4
9.2.2 The Data Flow Analysis Schema Data flow values • IN[ s ]: before statement s • OUT[ s ]: after statement s • Transfer function f s – forward: OUT[ s ] = f s (IN[ s ]) – backward: IN[ s ] = f s (OUT[ s ]) 5
Computing Reaching Definitions • Effect of single definition d : u = v op w : – OUT[ d ] = { d } ∪ (IN[ d ] − . . . ) 6
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, f d ( x ) = { d } ∪ ( x − { all other definitions of u in program } ) = gen d ∪ ( x − kill d ) where = { d } gen d = { all other definitions of u in program } kill d 7
Computing Reaching Definitions Effect of block B , with definitions 1 , 2 , . . . , n : { n, n − 1 , . . . , 1 } − { definitions killed afterwards } = gen B gen n ∪ ( gen n − 1 − kill n ) ∪ ( gen n − 2 − kill n − 1 − kill n ) . . . = = kill 1 ∪ kill 2 ∪ . . . ∪ kill n kill B 8
Computing Reaching Definitions ENTRY ❄ gen B 1 = { d 1 , d 2 , d 3 } d 1 : i = m-1 d 2 : j = n kill B 1 = { d 4 , d 5 , d 6 , d 7 } B 1 d 3 : a = u1 ✬ ✲ ❄ gen B 2 = { d 4 , d 5 } d 4 : i = i+1 B 2 d 5 : j = j-1 kill B 2 = { d 1 , d 2 , d 7 } ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ✟ gen B 3 = { d 6 } d 6 : a = u2 B 3 kill B 3 = { d 3 } ❍❍❍❍❍❍❍❍❍ ❥ ❍ ❄ gen B 4 = { d 7 } ✫ d 7 : i = u3 B 4 kill B 4 = { d 1 , d 4 } ❄ EXIT In this case, gen B . . . 9
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 B OUT[ P ] OUT[ B ] = gen B ∪ (IN[ B ] − kill B ) } Typical form of algorithm for forward data-flow analysis ∪ is meet operator (why ∪ ?) Example with B = B 1 , B 2 , B 3 , B 4 , EXIT. . . 10
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 B OUT[ P ] OUT[ B ] = gen B ∪ (IN[ B ] − kill B ) } 11
Implementation of Iterative Algorithm for Computing Reaching Definitions With bit vectors OUT[ B ] 0 IN[ B ] 1 OUT[ B ] 1 IN[ B ] 2 OUT[ B ] 2 Block B 000 0000 000 0000 111 0000 000 0000 111 0000 B 1 000 0000 111 0000 001 1100 111 0111 001 1110 B 2 000 0000 001 1100 000 1110 001 1110 000 1110 B 3 000 0000 001 1110 001 0111 001 1110 001 0111 B 4 EXIT 000 0000 000 0000 001 0111 001 0111 001 0111 12
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 B OUT[ P ] OUT[ B ] = gen B ∪ (IN[ B ] − kill B ) } Fixed point / steady state Often, solution depends on initialization 13
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
Live-Variable Analysis Effect of block B on live variables • use B : 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 of that variable in B ( ≈ kill ) 15
Computing Live Variables ENTRY ❄ use B 1 = { m, n, u 1 } d 1 : i = m-1 d 2 : j = n def B 1 = { i, j, a } B 1 d 3 : a = u1 ✬ ✲ ❄ d 4 : i = i+1 use B 2 = { i, j } B 2 d 5 : j = j-1 def B 2 = ∅ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✟ ✙ ✟ ✟ use B 3 = { u 2 } d 6 : a = u2 B 3 def B 3 = { a } ❍❍❍❍❍❍❍❍❍ ❥ ❍ ❄ use B 4 = { u 3 } ✫ d 7 : i = u3 B 4 def B 4 = { i } ❄ EXIT 16
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 B IN[ S ] IN[ B ] = use B ∪ (OUT[ B ] − def B ) } Typical form of algorithm for backward data-flow analysis 17
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
Available Expressions (Example) B 1 t1 = 4*i ❄ t3 = 4*i B 3 19
Available Expressions (Example) B 1 t1 = 4*i ✟ ✟ ✟ ✟ ✟ ✙ B 2 ? ❍❍❍❍ ❍ ❥ ❄ t3 = 4*i B 3 20
Available Expressions (Example) B 1 t1 = 4*i ✟ ✟ ✟ ✟ ✟ ✙ i = 17 B 2 ❍❍❍❍ ❍ ❥ ❄ t3 = 4*i B 3 21
Available Expressions (Example) B 1 t1 = 4*i ✟ ✟ ✟ ✟ ✟ ✙ i = 17 B 2 t2 = 4*i ❍❍❍❍ ❍ ❥ ❄ t3 = 4*i B 3 22
Available Expressions (Example) B 1 t1 = 4*i ✟ ✟ ✟ ✟ ✟ ✙ i = 17 B 2 t1 = 4*i ❍❍❍❍ ❍ ❥ ❄ t3 = 4*i B 3 23
Computing Available Expressions Effect of block B on available expressions • e gen B : expressions y op z that are definitely computed in B , and for which y and z are not subsequently redefined • e kill B : expressions y op z for which y and/or z are (or may be) defined in B , and that are not subsequently recomputed 24
Computing e gen B (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
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 B OUT[ P ] OUT[ B ] = e gen B ∪ (IN[ B ] − e kill B ) } Why U . . . 26
Available Expressions (Example) ENTRY ❄ B 1 a = b+c ✬ ✲ ❄ d = a+e B 2 ✫ e = d+c ❄ d = b+c B 3 ❄ EXIT 27
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 B OUT[ P ] OUT[ B ] = gen B ∪ (IN[ B ] − kill B ) } Order of blocks in second for-loop matters 28
Efficient Iterative Data-Flow Analysis ✛✘ ✬ ✲ 1 ✚✙ ✛✘ ✟ ✟ ✟ ✟ ✙ ✟ 2 ✚✙ ✛✘ ❍❍❍❍ ❄ ✬ ❥ ❍ ✲ 3 ✚✙ ✻ ✛✘ ❄ ✩ ✛ 4 ✚✙ ✛✘ ✛✘ ✟ ❍❍❍❍ ✟ ✟ ✟ ✟ ✙ ❍ ❥ ✚✙ 5 ✚✙ 6 ✛✘ ❍❍❍❍ ✟ ✟ ✟ ✪ ✟ ❍ ❥ ✙ ✟ ✚✙ 7 ❪ ❏ ❏ ❏ ✛✘ ❏ ❄ ✫ ❏ ❏ ✚✙ 8 ✛✘ ✛✘ ✟ ❍❍❍❍ ❏ ✟ ✟ ❏ ✫ ✟ ✙ ✟ ❥ ❍ ✚✙ 9 ✚✙ 10 Order of blocks in second for-loop matters 29
Exercises 30
Flow Graph For Data Flow Analysis ENTRY ❄ (1) a = 1 B 1 (2) b = 2 ✩ ❄ ✛ (3) c = a+b (4) d = c-a B 2 ✏ ✏ ✏ ✬ ✏ ✏ ✏ ✲ ✮ ✏ (5) d = b+d PPPPPP B 3 P q B 5 (8) b = a+b ❄ B 4 ✪ (6) d = a+b (9) e = c-a ✫ (7) e = e+1 ❄ (10) a = b+d B 6 (11) b = a-d ❄ EXIT 31
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
Flow Graph G DFST domination PPPPPPPPPPPPPPPPP ✟ ❆ ✟ ✟ ❆ ✟ ✟ ❆ ✟ ✟ ❆ ✟ ✟ ❆ ✟ ✟ ❯ ❆ ✟ ✙ ❄ P q back edge DFO retreating edge ❄ ❄ natural loop order of nodes ❄ single entry in iteration DFA depth DFST ❆ ✁ ❆ ✁ ❆ ✁ ❆ ✁ ❆ ✁ ❄ ❯ ❆ ✁ ☛ inside-out 1 + depth DFST optimization iterations DFA 33
Recommend
More recommend