Download presentation
Presentation is loading. Please wait.
Published byBelinda Bertha Wells Modified over 9 years ago
1
Compiler Principles Fall 2014-2015 Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University
2
2 סקר הוראה
3
Tentative syllabus Front End Scanning Top-down Parsing (LL) Bottom-up Parsing (LR) Intermediate Representation Lowering Optimizations Local Optimizations Dataflow Analysis Loop Optimizations Code Generation Register Allocation Instruction Selection 3 mid-termexam
4
Previously Dataflow framework Global constant propagation 4
5
agenda Finish dataflow framework – Monotone frameworks – Distributivity Reaching definitions Loop code motion (Strength reduction via induction variables) 5
6
6 Dataflow framework reminder
7
Join semilattice definition A join semilattice is a pair (V, ), where V is a set of elements (domain) is a join operator that is – commutative: x y = y x – associative: (x y) z = x (y z) – idempotent: x x = x If x y = z, we say that z is the join or (Least Upper Bound) of x and y Every join semilattice has a bottom element denoted such that x = x for all x 7
8
Partial ordering induced by join Every join semilattice (V, ) induces an ordering relationship over its elements Define x y iff x y = y Need to prove – Reflexivity: x x – Antisymmetry: If x y and y x, then x = y – Transitivity: If x y and y z, then x z 8
9
Join semilattice example for liveness 9 {} {a}{b}{c} {a, b}{a, c}{b, c} {a, b, c} Bottom element
10
Dataflow framework A global analysis is a tuple ( D, V, , F, I ), where –D is a direction (forward or backward) The order to visit statements within a basic block, NOT the order in which to visit the basic blocks –V is a set of values (sometimes called domain) – is a join operator over those values –F is a set of transfer functions f s : V V (for every statement s) –I is an initial value 10
11
Running global analyses Assume that ( D, V, , F, I ) is a forward analysis For every statement s maintain values before - IN[s] - and after - OUT[s] Set OUT[s] = for all statements s Set OUT[entry] = I Repeat until no values change: – For each statement s with predecessors PRED[s]={p 1, p 2, …, p n } Set IN[s] = OUT[p 1 ] OUT[p 2 ] … OUT[p n ] Set OUT[s] = f s (IN[s]) The order of this iteration does not matter – Chaotic iteration 11
12
Soundness of dataflow analysis The result of the fixed-point algorithm is sound – holds for every execution of the program Sometimes coming up with precise join and transfer functions is hard, but we can always do with more conservative versions – Define x y x y An operator that returns some upper bound – not necessarily the least one – Example, liveness {a, b, c} {b, c, d} is (union) {a, b, c} – We can define a more conservative upper bound operation X Y = (X Y) {e} 12
13
13 Termination Reasoning for dataflow analysis
14
Proving termination Our algorithm for running these analyses continuously loops until no changes are detected Problem: how do we know the analyses will eventually terminate? 14
15
A non-terminating analysis The following analysis will loop infinitely on any CFG containing a loop: Direction: Forward Domain: ℕ Join operator: max Transfer function: f(n) = n + 1 Initial value: 0 15
16
A non-terminating analysis 16 start end x = y
17
Initialization 17 start end x = y 0 0
18
Fixed-point iteration 18 start end x = y 0 0
19
Choose a block 19 start end x = y 0 0
20
Iteration 1 20 start end x = y 0 0 0
21
Iteration 1 21 start end x = y 1 0 0
22
Choose a block 22 start end x = y 1 0 0
23
Iteration 2 23 start end x = y 1 0 0
24
Iteration 2 24 start end x = y 1 0 1
25
Iteration 2 25 start end x = y 2 0 1
26
Choose a block 26 start end x = y 2 0 1
27
Iteration 3 27 start end x = y 2 0 1
28
Iteration 3 28 start end x = y 2 0 2
29
Iteration 3 29 start end x = y 3 0 2
30
Why doesn’t this terminate? Values can increase without bound Note that “increase” refers to the lattice ordering, not the ordering on the natural numbers The height of a semilattice is the length of the longest increasing sequence in that semilattice The dataflow framework is not guaranteed to terminate for semilattices of infinite height Note that a semilattice can be infinitely large but have finite height – e.g. constant propagation 30 0 1 2 3 4...
31
Height of a lattice An increasing chain is a sequence of elements a 1 a 2 … a k – The length of such a chain is k The height of a lattice is the length of the maximal increasing chain For liveness with n program variables: – {} {v 1 } {v 1,v 2 } … {v 1,…,v n } For available expressions it is the number of expressions of the form a=b op c – For n program variables and m operator types: O(m n 3 ) 31
32
32 Monotonicity
33
Another non-terminating analysis This analysis works on a finite-height semilattice, but will not terminate on certain CFGs: Direction: Forward Domain: Boolean values true and false Join operator: Logical OR Transfer function: Logical NOT Initial value: false 33
34
A non-terminating analysis 34 start end x = y
35
Initialization 35 start end x = y false
36
Fixed-point iteration 36 start end x = y false
37
Choose a block 37 start end x = y false
38
Iteration 1 38 start end x = y false
39
Iteration 1 39 start end x = y true false
40
Iteration 2 40 start end x = y true false true
41
Iteration 2 41 start end x = y false true
42
Iteration 3 42 start end x = y false
43
Iteration 3 43 start end x = y true false
44
Why doesn’t it terminate? Values can loop indefinitely Intuitively, the join operator keeps pulling values up If the transfer function can keep pushing values back down again, then the values might cycle forever 44 false true false true false...
45
Why doesn’t it terminate? Values can loop indefinitely Intuitively, the join operator keeps pulling values up If the transfer function can keep pushing values back down again, then the values might cycle forever How can we fix this? 45 false true false true false...
46
Monotone transfer functions A transfer function f is monotone iff if x y, then f(x) f(y) Intuitively, if you know less information about a program point, you can't “gain back” more information about that program point Many transfer functions are monotone, including those for liveness and constant propagation Note: Monotonicity does not mean that x f(x) – (This is a different property called extensivity) 46
47
Liveness and monotonicity A transfer function f is monotone iff if x y, then f(x) f(y) Recall our transfer function for a = b + c is – f a = b + c (V) = (V – {a}) {b, c} Recall that our join operator is set union and induces an ordering relationship X Y iff X Y Is this monotone? 47
48
Is constant propagation monotone? A transfer function f is monotone iff if x y, then f(x) f(y) Recall our transfer functions – f x=k (V) = V[x k] (update V by mapping x to k) – f x=y (V) = V[x V(y)] (update val. of x to val. of y) – f x=a+b (V) = V[x ] (assign Not-a-Constant) Is this monotone? 48 0-212...
49
The grand result Theorem: A dataflow analysis with a finite- height semilattice and family of monotone transfer functions always terminates Proof sketch: – The join operator can only bring values up – Transfer functions can never lower values back down below where they were in the past (monotonicity) – Values cannot increase indefinitely (finite height) 49
50
50 distributivity
51
An “optimality” result A transfer function f is distributive if f(a b) = f(a) f(b) for every domain elements a and b If all transfer functions are distributive then the fixed-point solution is equal to the solution computed by joining results from all (potentially infinite) control-flow paths – Join over all paths Optimal if we ignore program conditions – Pretend all control-flow paths can be executed by the program Which analyses use distributive functions? 51
52
An “optimality” result A transfer function f is distributive if f(a b) = f(a) f(b) for every domain elements a and b If all transfer functions are distributive then the fixed-point solution is equal to the solution computed by joining results from all (potentially infinite) control-flow paths – Join over all paths Optimal if we ignore program conditions – Pretend all control-flow paths can be executed by the program Which analyses use distributive functions? 52
53
53 Loop optimizations
54
Most of a program’s computations are done inside loops – Focus optimizations effort on loops The optimizations we’ve seen so far are independent of the control structure Some optimizations are specialized to loops – Loop-invariant code motion – (Strength reduction via induction variables) Require another type of analysis to find out where expressions get their values from – Reaching definitions 54
55
Loop invariant computation 55 y = t * 4 x < y + z end x = x + 1 start y = … t = … z = …
56
Loop invariant computation 56 y = t * 4 x < y + z end x = x + 1 start y = … t = … z = … t*4 and y+z have same value on each iteration
57
Code hoisting 57 x < w end x = x + 1 start y = … t = … z = … y = t * 4 w = y + z
58
What reasoning did we use? 58 y = t * 4 x < y + z end x = x + 1 start y = … t = … z = … y is defined inside loop but it is loop invariant since t*4 is loop-invariant Both t and z are defined only outside of loop constants are trivially loop-invariant
59
What about now? 59 y = t * 4 x < y + z end x = x + 1 t = t + 1 start y = … t = … z = … Now t is not loop-invariant and so are t*4 and y
60
Loop-invariant code motion d: t = a 1 op a 2 – d is a program location a 1 op a 2 loop-invariant (for a loop L) if computes the same value in each iteration – Hard to know in general Conservative approximation – Each a i is a constant, or – All definitions of a i that reach d are outside L, or – Only one definition of of a i reaches d, and is loop- invariant itself Transformation: hoist the loop-invariant code outside of the loop 60
61
61 Reaching definitions analysis
62
A definition d: t = … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Let’s define the corresponding dataflow analysis… 62
63
Reaching definitions analysis A definition d: t = … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Direction: ? Domain: ? Join operator: ? Transfer function: f d: a=b op c (RD) = ? f d: not-a-def (RD) = ? – Where defs(a) is the set of locations defining a (statements of the form a=...) Initial value: ? 63
64
Reaching definitions analysis A definition d: t = … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Direction: Forward Domain: sets of program locations that are definitions Join operator: union Transfer function: f d: a=b op c (RD) = (RD - defs(a)) {d} f d: not-a-def (RD) = RD – Where defs(a) is the set of locations defining a (statements of the form a=...) Initial value: {} 64
65
Reaching definitions analysis 65 d4: y = t * 4 d4:x < y + z d6: x = x + 1 d1: y = … d2: t = … d3: z = … start end {}
66
Reaching definitions analysis 66 d4: y = t * 4 d4:x < y + z d5: x = x + 1 start d1: y = … d2: t = … d3: z = … end {}
67
Initialization 67 d4: y = t * 4 d4:x < y + z d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} end {}
68
Iteration 1 68 d4: y = t * 4 d4:x < y + z d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} end {}
69
Iteration 1 69 d4: y = t * 4 d4:x < y + z d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1} {d1, d2} {d1, d2, d3} end {}
70
Iteration 2 70 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1} {d1, d2} {d1, d2, d3} {}
71
Iteration 2 71 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {} {d1} {d1, d2} {d1, d2, d3} {}
72
Iteration 2 72 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {}
73
Iteration 2 73 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {}
74
Iteration 3 74 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {d2, d3, d4} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {}
75
Iteration 3 75 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {d2, d3, d4} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {d2, d3, d4, d5}
76
Iteration 4 76 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3} {d2, d3, d4} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {d2, d3, d4, d5}
77
Iteration 4 77 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3, d4, d5} {d2, d3, d4} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4} {d2, d3, d4, d5}
78
Iteration 4 78 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3, d4, d5} {d2, d3, d4} {} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4, d5}
79
Iteration 5 79 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4, d5} {d1} {d1, d2} {d1, d2, d3} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5} d4: y = t * 4 x < y + z {d1, d2, d3, d4, d5} {d2, d3, d4, d5}
80
Iteration 6 80 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4, d5} {d1} {d1, d2} {d1, d2, d3} d5: x = x + 1 {d2, d3, d4, d5} d4: y = t * 4 x < y + z {d1, d2, d3, d4, d5} {d2, d3, d4, d5}
81
Which expressions are loop invariant 81 t is defined only in d2 – outside of loop z is defined only in d3 – outside of loop y is defined only in d4 – inside of loop but depends on t and 4, both loop-invariant start d1: y = … d2: t = … d3: z = … {} {d1} {d1, d2} {d1, d2, d3} end {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5} d4: y = t * 4 x < y + z {d1, d2, d3, d4, d5} {d2, d3, d4, d5} x is defined only in d5 – inside of loop so is not a loop-invariant
82
82 Inferring loop invariants
83
Inferring loop-invariant expressions For a statement s of the form t = a 1 op a 2 A variable a i is immediately loop-invariant if all reaching definitions IN[s]={d 1,…,d k } for a i are outside of the loop LOOP-INV = immediately loop-invariant variables and constants LOOP-INV = LOOP-INV {x | d: x = a 1 op a 2, d is in the loop, and both a 1 and a 2 are in LOOP-INV} – Iterate until fixed-point An expression is loop-invariant if all operands are loop-invariants 83
84
Computing LOOP-INV 84 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5}
85
Computing LOOP-INV 85 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5} (immediately) LOOP-INV = {t}
86
Computing LOOP-INV 86 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5} (immediately) LOOP-INV = {t, z}
87
Computing LOOP-INV 87 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5} (immediately) LOOP-INV = {t, z}
88
Computing LOOP-INV 88 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5} (immediately) LOOP-INV = {t, z}
89
Computing LOOP-INV 89 end start d1: y = … d2: t = … d3: z = … {} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} LOOP-INV = {t, z, 4} d4: y = t * 4 x < y + z d5: x = x + 1 {d1, d2, d3, d4, d5} {d2, d3, d4, d5}
90
Computing LOOP-INV 90 d4: y = t * 4 x < y + z end d5: x = x + 1 start d1: y = … d2: t = … d3: z = … {} {d1, d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4} {d1} {d1, d2} {d1, d2, d3} {d2, d3, d4, d5} LOOP-INV = {t, z, 4, y}
91
91 Strength reduction via induction variables
92
Induction variables 92 while (i < x) { j = a + 4 * i a[j] = j i = i + 1 } i is incremented by a loop-invariant expression on each iteration – this is called an induction variable j is a linear function of the induction variable with multiplier 4
93
Strength-reduction 93 j = a + 4 * (i-1) while (i < x) { j = j + 4 a[j] = j i = i + 1 } Prepare initial value Increment by multiplier
94
Summary of optimizations 94 Enabled OptimizationsAnalysis Common-subexpression elimination Copy Propagation Available Expressions Constant foldingConstant Propagation Dead code eliminationLive Variables Loop-invariant code motionReaching Definitions
95
95 Dataflow analysis summary
96
Join semilattice A join semilattice is a pair (V, ), where V is a set of elements is a join operator that is – commutative: x y = y x – associative: (x y) z = x (y z) – idempotent: x x = x If x y = z, we say that z is the join or (Least Upper Bound) of x and y Every join semilattice has a bottom element denoted such that x = x for all x 96
97
Join semilattices and orderings Every join semilattice (V, ) induces an ordering relationship over its elements Define x y iff x y = y Need to prove – Reflexivity: x x – Antisymmetry: If x y and y x, then x = y – Transitivity: If x y and y z, then x z 97
98
Hasse diagram 98 {} {a}{b}{c} {a, b}{a, c}{b, c} {a, b, c} Greater Lower
99
Dataflow framework A (global) dataflow analysis is a tuple ( D, V, , F, I ) –D is a direction (forward or backward) The order to visit statements within a basic block –V is a set of lattice elements – is a join operator over the lattice elements –F is a set of transfer functions f : V V One per statement –I is an initial value 99
100
Running global analyses Assume that ( D, V, , F, I ) is a forward analysis Set OUT[s] = for all statements s Set OUT[entry] = I Repeat until no values change: – For each statement s with predecessors p 1, p 2, …, p n : Set IN[s] = OUT[p 1 ] OUT[p 2 ] … OUT[p n ] Set OUT[s] = f s (IN[s]) The order of this iteration does not matter: Chaotic iteration (due to Garry Kildall) 100
101
Height of a lattice An increasing chain is a sequence of elements a 1 a 2 … a k – The length of such a chain is k The height of a lattice is the length of the maximal increasing chain 101
102
Properties of transfer functions A transfer function f is monotone iff if x y, then f(x) f(y) A transfer function f is distributive iff f(a b) = f(a) f(b) for every domain elements a and b 102
103
Dataflow analysis – main theorem Theorem 1: A dataflow analysis with a finite- height semilattice and family of monotone transfer functions always terminates Theorem 2: the solution to the system of equations is unique – the minimal fixed point in the lattice 103
104
Analysis precision Ideal solution – Only consider execution paths that are possible at runtime – Undecidable to find these paths statically Join over all paths (JOP) – Assuming every control flow path is feasible – Less precise solution but conservative (sound) – If all transfer functions are distributive Least fixed point – Less precise than JOP but sound – The result of any dataflow analysis 104
105
Analysis direction Forward – In the CFG, information is propagated from predecessors of a statement to its output – Properties depend on past computations – Examples: available expressions, constant propagating, reaching definitions Backward In the CFG, information is propagated from successors of a statement to its output Properties depend on future computations Examples: Liveness analysis 105
106
Types of dataflow analysis May vs. Must – Must analysis – properties hold on all paths (join is set intersection) Examples: available expressions, constant propagation – May analysis – properties hold on some path (join is set union) Examples: liveness, reaching definitions 106
107
107 Global optimizations practice
108
108 2014 מועד א '
109
109
110
110 false true x < 0x = 0x > 0 x 0x 0 פתרון
111
111
112
112 פתרון
113
113
114
פתרון 114
115
115 2013 מועד א '
116
116
117
117
118
Aliasing dataflow problem Direction D = forward Domain MayPointTo = {x = &y | x is a pointer variable and y is a numeric variable} Join operator = Transfer functions – For two numeric variables x and y: f x=y (M ) = M – For two pointer variables x and y: f x=y (M ) = (M \ {x = &z | z is a numeric variable}) {x = &z | y = &z M} – f x=&y (M ) = (M \ {x = &z | z is a numeric variable}) {x=&y} – f x=y+z (M ) = f x=*y (M ) = f *x=y (M ) = M Initial value I = {} 118
119
The lattice is finite – there are O(n 2 ) facts of the form x = &y for n program variables. Therefore the height of the lattice is finite. The transfer functions have a GEN-KILL type: f(X) = (X \ A) B, which we already proved to be monotone in class 119
120
120
121
121
122
122
123
123
124
124
125
125 2013 מועד ב '
126
126
127
Next lecture: Register Allocation
128
128 2013 מועד ב '
129
119
130
120
131
131
132
132
133
133
134
134
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.