Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and...
-
Upload
alfred-carter -
Category
Documents
-
view
215 -
download
0
Transcript of Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and...
![Page 1: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/1.jpg)
Winter 2012-2013Compiler Principles
Global Optimizations
Mayer Goldberg and Roman ManevichBen-Gurion University
![Page 2: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/2.jpg)
2
Today
• Review local analyses– Available expressions– Live variables
• Review local optimizations– Common Sub-expression elimination and copy
propagation– Dead code elimination
• Global analysis and optimization– The dataflow framework
![Page 3: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/3.jpg)
3
Local analysis framework
• Define an analysis of a basic block as a quadruple (D, V, F, I) where– D is a direction (forwards or backwards)– V is a set of values the program can have at any
point– F is a family of transfer functions defining the
meaning of any expression as a function f : V V– I is the initial information at the top (or bottom)
of a basic block
![Page 4: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/4.jpg)
4
Simplifying assumptions
• Assume code does not allow taking address of variables
• Ignore memory operations• Ignore function calls
![Page 5: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/5.jpg)
5
Available Expressions
• An expression is said to be available at some point in the program if some program variable holds the value of that expression
• We want infer equalities of the formsx = yx = op y (for unary operators)x = y op z (for binary operators)
![Page 6: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/6.jpg)
6
Available Expressions
• Direction: Forward• Values: Sets of expressions assigned to variables• Transfer functions: Given a set of variable
assignments V and statement a = b + c:– Remove from V any expression containing a as a
subexpression– Add to V the expression a = b + c– Formally: Vout = (Vin \ {e | e contains a}) {a = b + c}
• Initial value: Empty set of expressions
![Page 7: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/7.jpg)
7
Liveness Analysis
• A variable is live at a point in a program if later in the program its value will be read before it is written to again
![Page 8: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/8.jpg)
8
Liveness Analysis• Direction: Backward• Values: Sets of variables• Transfer functions: Given a set of variable assignments V
and statement a = b + c:• Remove a from V (any previous value of a is now dead)• Add b and c to V (any previous value of b or c is now live)• Formally: Vin = (Vout \ {a}) {b,c}• Initial value: Depends on semantics of language
– E.g., function arguments and return values (pushes)– Result of local analysis of other blocks as part of a global
analysis
![Page 9: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/9.jpg)
Running local analyses
• Given an analysis (D, V, F, I) for a basic block• Assume that D is “forward” analogous for the
reverse case• Initially, set OUT[entry] to I• For each statement s, in order:– Set IN[s] to OUT[prev], where prev is the previous
statement– Set OUT[s] to fs(IN[s]), where fs is the transfer
function for statement s
9
![Page 10: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/10.jpg)
10
Available expressions analysis
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;
sIN[s] OUT[s]
{a=b, c=b, d=b, e=a+b, f=a+b}
{a=b, c=b, d=b, e=a+b}
{a=b, c=b, d=a+b, e=a+b}
{a=b, c=b, d=a+b}
{a=b, c=b}
{a=b}
entry: {} Initial value
![Page 11: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/11.jpg)
11
Optimizing from available expressions
• Common sub-expression elimination– If {… t = y op z … } x = y op z– Can transform statement into x = t
• Copy propagation– If {… y = t … } x = y op z– Can transform statement into x = t op z
• Note: same for x=y and x=op y
![Page 12: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/12.jpg)
Liveness analysis
12
sIN[s] OUT[s]a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
{ b, d } exit:
{ b, d, e }
{ a, b, e }
{ a, b, d }
{ a, b }
{ a, b }
{ b }
Initial value
![Page 13: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/13.jpg)
13
Optimizing from liveness analysis
• Dead code elimination– If x = y op z {v1,…,vk}
– And x {v1,…,vk}
– We can eliminate x = y op z• Note: same for x=y and x=op y
![Page 14: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/14.jpg)
14
Quiz: how to enable this optimization?
a = b;
c = b;
d = a + b;
e = a + c; {a=b, c=b, d=a+b, e=a+c}
{a=b, c=b, d=a+b}
{a=b, c=b}
{a=b}
entry: {}
OUT[s]
d;
Notice that c=b and d=a+b implies a+c=a+b
![Page 15: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/15.jpg)
15
Answer
• Define a closure operator– Closure(AE) =
if exist x, y, z, w such that {x=y, z=x op w}AE return Closure( AE {z=y op w} ) else return AE
– If op is commutative can also add {z=w op y}• The idea is to make implicit equalities explicit by
substituting all equal variables• Now redefine every transfer function f of
available expressions to f’ = Closure(f)
![Page 16: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/16.jpg)
Global Optimizations
16
![Page 17: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/17.jpg)
17
Global analysis
• A global analysis is an analysis that works on a control-flow graph as a whole
• Substantially more powerful than a local analysis– (Why?)
• Substantially more complicated than a local analysis– (Why?)
![Page 18: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/18.jpg)
18
Local vs. global analysis• Many of the optimizations from local analysis can still
be applied globally– Common sub-expression elimination– Copy propagation– Dead code elimination
• Certain optimizations are possible in global analysis that aren't possible locally:– e.g. code motion: Moving code from one basic block into
another to avoid computing values unnecessarily• Example global optimizations:– Global constant propagation– Partial redundancy elimination
![Page 19: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/19.jpg)
19
Loop invariant code motion example
while (t < 120) { z = z + x - y;}
w = x – y;while (t < 120) { z = z + w;}
value of expression x – y is not changed by loop body
![Page 20: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/20.jpg)
20
Why global analysis is hard
• Need to be able to handle multiple predecessors/successors for a basic block
• Need to be able to handle multiple paths through the control-flow graph, and may need to iterate multiple times to compute the final value (but the analysis still needs to terminate!)
• Need to be able to assign each basic block a reasonable default value for before we've analyzed it
![Page 21: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/21.jpg)
21
Global dead code elimination
• Local dead code elimination needed to know what variables were live on exit from a basic block
• This information can only be computed as part of a global analysis
• How do we modify our liveness analysis to handle a CFG?
![Page 22: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/22.jpg)
22
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
![Page 23: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/23.jpg)
23
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{a, c, d}
?
Which variables may be live on some execution path?
![Page 24: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/24.jpg)
24
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{a, c, d}
![Page 25: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/25.jpg)
25
CFGs without loops
Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
![Page 26: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/26.jpg)
26
CFGs without loops
Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
![Page 27: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/27.jpg)
27
Major changes – part 1
• In a local analysis, each statement has exactly one predecessor
• In a global analysis, each statement may have multiple predecessors
• A global analysis must have some means of combining information from all predecessors of a basic block
![Page 28: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/28.jpg)
28
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{b, c, d}
{c, d}Need to combine currently-computed value with new value
Need to combine currently-computed value with new value
![Page 29: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/29.jpg)
29
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{c, d}
![Page 30: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/30.jpg)
30
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{a, c, d}
![Page 31: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/31.jpg)
31
Major changes – part 2
• In a local analysis, there is only one possible path through a basic block
• In a global analysis, there may be many paths through a CFG
• May need to recompute values multiple times as more information becomes available
• Need to be careful when doing this not to loop infinitely!– (More on that later)
![Page 32: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/32.jpg)
32
CFGs with loops• Up to this point, we've considered loop-free CFGs,
which have only finitely many possible paths• When we add loops into the picture, this is no longer
true• Not all possible loops in a CFG can be realized in the
actual program
IfZ x goto Top
x = 1;
Top:
x = 0;
x = 2;
![Page 33: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/33.jpg)
33
CFGs with loops• Up to this point, we've considered loop-free CFGs, which
have only finitely many possible paths• When we add loops into the picture, this is no longer true• Not all possible loops in a CFG can be realized in the actual
program• Sound approximation: Assume that every possible path
through the CFG corresponds to a valid execution– Includes all realizable paths, but some additional paths as well– May make our analysis less precise (but still sound)– Makes the analysis feasible; we'll see how later
![Page 34: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/34.jpg)
34
CFGs with loops
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;IfZ ...
Entry
{a}
?
![Page 35: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/35.jpg)
35
Major changes – part 3
• In a local analysis, there is always a well defined “first” statement to begin processing
• In a global analysis with loops, every basic block might depend on every other basic block
• To fix this, we need to assign initial values to all of the blocks in the CFG
![Page 36: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/36.jpg)
36
CFGs with loops - initialization
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{}
![Page 37: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/37.jpg)
37
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{}
{a}
![Page 38: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/38.jpg)
38
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{a, b, c}
{a}
![Page 39: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/39.jpg)
39
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{a, b, c}
{a}
{a, b, c}
![Page 40: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/40.jpg)
40
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{}
{a, b, c}
{a}
{a, b, c}
![Page 41: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/41.jpg)
41
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{}
{a, b, c}
{a}
{a, b, c}
{b, c}
![Page 42: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/42.jpg)
42
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
![Page 43: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/43.jpg)
43
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
![Page 44: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/44.jpg)
44
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
![Page 45: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/45.jpg)
45
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
![Page 46: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/46.jpg)
46
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
![Page 47: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/47.jpg)
47
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
![Page 48: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/48.jpg)
48
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
![Page 49: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/49.jpg)
49
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
![Page 50: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/50.jpg)
50
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{a, c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
![Page 51: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/51.jpg)
51
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{a, c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
![Page 52: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/52.jpg)
52
Summary of differences
• Need to be able to handle multiple predecessors/successors for a basic block
• Need to be able to handle multiple paths through the control-flow graph, and may need to iterate multiple times to compute the final value– But the analysis still needs to terminate!
• Need to be able to assign each basic block a reasonable default value for before we've analyzed it
![Page 53: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/53.jpg)
53
Global liveness analysis
• Initially, set IN[s] = { } for each statement s• Set IN[exit] to the set of variables known to be
live on exit (language-specific knowledge)• Repeat until no changes occur:– For each statement s of the form a = b + c, in any
order you'd like:• Set OUT[s] to set union of IN[p] for each successor p of s• Set IN[s] to (OUT[s] – a) {b, c}.
• Yet another fixed-point iteration!
![Page 54: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/54.jpg)
54
Global liveness analysis
a=b+c
s2 s3
IN[s2] IN[s3]
OUT[s]=IN[s2] IN[s3]
IN[s]=(UT[s] – {a}) {b, c}
![Page 55: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/55.jpg)
55
Why does this work?• To show correctness, we need to show that
– The algorithm eventually terminates, and– When it terminates, it has a sound answer
• Termination argument:– Once a variable is discovered to be live during some point of the
analysis, it always stays live– Only finitely many variables and finitely many places where a
variable can become live• Soundness argument (sketch):
– Each individual rule, applied to some set, correctly updates liveness in that set
– When computing the union of the set of live variables, a variable is only live if it was live on some path leaving the statement
![Page 56: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/56.jpg)
56
Theory to the rescue
• Building up all of the machinery to design this analysis was tricky
• The key ideas, however, are mostly independent of the analysis:– We need to be able to compute functions describing
the behavior of each statement– We need to be able to merge several subcomputations
together– We need an initial value for all of the basic blocks
• There is a beautiful formalism that captures many of these properties
![Page 57: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/57.jpg)
57
Join semilattices• A join semilattice is a ordering defined on a set of
elements• Any two elements have some join that is the smallest
element larger than both elements• There is a unique bottom element, which is smaller
than all other elements• Intuitively:– The join of two elements represents combining information
from two elements by an overapproximation• The bottom element represents “no information yet”
or “the least conservative possible answer”
![Page 58: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/58.jpg)
58
Join semilattice for liveness
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
Bottom element
![Page 59: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/59.jpg)
59
What is the join of {b} and {c}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 60: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/60.jpg)
60
What is the join of {b} and {c}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 61: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/61.jpg)
61
What is the join of {b} and {a,c}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 62: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/62.jpg)
62
What is the join of {b} and {a,c}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 63: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/63.jpg)
63
What is the join of {a} and {a,b}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 64: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/64.jpg)
64
What is the join of {a} and {a,b}?
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}
![Page 65: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/65.jpg)
65
Formal definitions
• A join semilattice is a pair (V, ), where• V is a domain 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 joinor (least upper bound) of x and y
• Every join semilattice has a bottom element denoted such that x = x for all x
![Page 66: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/66.jpg)
66
Join semilattices and ordering
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}Greater
Lower
![Page 67: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/67.jpg)
67
Join semilattices and ordering
{}
{a} {b} {c}
{a, b} {a, c} {b, c}
{a, b, c}Least precise
Most precise
![Page 68: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/68.jpg)
68
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
![Page 69: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/69.jpg)
69
An example join semilattice
• The set of natural numbers and the max function• Idempotent– max{a, a} = a
• Commutative– max{a, b} = max{b, a}
• Associative– max{a, max{b, c}} = max{max{a, b}, c}
• Bottom element is 0:– max{0, a} = a
• What is the ordering over these elements?
![Page 70: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/70.jpg)
70
A join semilattice for liveness
• Sets of live variables and the set union operation• Idempotent:– x x = x
• Commutative:– x y = y x
• Associative:– (x y) z = x (y z)
• Bottom element:– The empty set: Ø x = x
• What is the ordering over these elements?
![Page 71: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/71.jpg)
71
Semilattices and program analysis
• Semilattices naturally solve many of the problems we encounter in global analysis
• How do we combine information from multiple basic blocks?
• What value do we give to basic blocks we haven't seen yet?
• How do we know that the algorithm always terminates?
![Page 72: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/72.jpg)
72
Semilattices and program analysis
• Semilattices naturally solve many of the problems we encounter in global analysis
• How do we combine information from multiple basic blocks?– Take the join of all information from those blocks
• What value do we give to basic blocks we haven't seen yet?– Use the bottom element
• How do we know that the algorithm always terminates?– Actually, we still don't! More on that later
![Page 73: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/73.jpg)
73
Semilattices and program analysis
• Semilattices naturally solve many of the problems we encounter in global analysis
• How do we combine information from multiple basic blocks?– Take the join of all information from those blocks
• What value do we give to basic blocks we haven't seen yet?– Use the bottom element
• How do we know that the algorithm always terminates?– Actually, we still don't! More on that later
![Page 74: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/74.jpg)
74
A general 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– is a join operator over those values– F is a set of transfer functions f : V V– I is an initial value
• The only difference from local analysis is the introduction of the join operator
![Page 75: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/75.jpg)
75
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
p1, p2, … , pn:• Set IN[s] = OUT[p1] OUT[p2] … OUT[pn]
• Set OUT[s] = fs (IN[s])
• The order of this iteration does not matter– This is sometimes called chaotic iteration
![Page 76: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/76.jpg)
76
For comparison
• Set OUT[s] = for all statements s
• Set OUT[entry] = I• Repeat until no values
change:– For each statement s
with predecessorsp1, p2, … , pn:• Set IN[s] = OUT[p1]
OUT[p2] … OUT[pn]
• Set OUT[s] = fs (IN[s])
• Set IN[s] = {} for all statements s
• Set OUT[exit] = the set of variables known to be live on exit
• Repeat until no values change:– For each statement s of the
form a=b+c:• Set OUT[s] = set union of IN[x]
for each successor x of s• Set IN[s] = (OUT[s]-{a}) {b,c}
![Page 77: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/77.jpg)
77
The dataflow framework
• This form of analysis is called the dataflow framework
• Can be used to easily prove an analysis is sound
• With certain restrictions, can be used to prove that an analysis eventually terminates– Again, more on that later
![Page 78: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/78.jpg)
78
Global constant propagation
• Constant propagation is an optimization that replaces each variable that is known to be a constant value with that constant
• An elegant example of the dataflow framework
![Page 79: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/79.jpg)
79
Global constant propagation
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
![Page 80: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/80.jpg)
80
Global constant propagation
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
![Page 81: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/81.jpg)
81
Global constant propagation
exit x = 4;
z = x;
w = 6;
y = 6; z = y;
x = 6;entry
![Page 82: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/82.jpg)
82
Constant propagation analysis
• In order to do a constant propagation, we need to track what values might be assigned to a variable at each program point
• Every variable will either– Never have a value assigned to it,– Have a single constant value assigned to it,– Have two or more constant values assigned to it, or– Have a known non-constant value.– Our analysis will propagate this information
throughout a CFG to identify locations where a value is constant
![Page 83: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/83.jpg)
83
Properties of constant propagation• For now, consider just some single variable x• At each point in the program, we know one of three
things about the value of x:– x is definitely not a constant, since it's been assigned two
values or assigned a value that we know isn't a constant– x is definitely a constant and has value k– We have never seen a value for x
• Note that the first and last of these are not the same!– The first one means that there may be a way for x to have
multiple values– The last one means that x never had a value at all
![Page 84: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/84.jpg)
84
Defining a join operator• The join of any two different constants is Not-a-Constant
– (If the variable might have two different values on entry to a statement, it cannot be a constant)
• The join of Not a Constant and any other value is Not-a-Constant– (If on some path the value is known not to be a constant, then on
entry to a statement its value can't possibly be a constant)• The join of Undefined and any other value is that other
value– (If x has no value on some path and does have a value on some
other path, we can just pretend it always had the assigned value)
![Page 85: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/85.jpg)
85
A semilattice for constant propagation
• One possible semilattice for this analysis is shown here (for each variable):
Undefined
0-1-2 1 2 ......
Not-a-constant
The lattice is infinitely wide
![Page 86: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/86.jpg)
86
A semilattice for constant propagation
• One possible semilattice for this analysis is shown here (for each variable):
Undefined
0-1-2 1 2 ......
Not-a-constant
• Note:• The join of any two different constants is Not-a-Constant• The join of Not a Constant and any other value is Not-a-Constant• The join of Undefined and any other value is that other value
![Page 87: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/87.jpg)
87
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;
y = x; z = y;
x = 6;entry
![Page 88: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/88.jpg)
88
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
x=Undefinedy=Undefinedz=Undefinedw=Undefined
![Page 89: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/89.jpg)
89
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
![Page 90: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/90.jpg)
90
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;Undefined
entryUndefined
![Page 91: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/91.jpg)
91
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6, y=z=w=
entryUndefined
![Page 92: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/92.jpg)
92
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6, y=z=w=
entryUndefined
![Page 93: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/93.jpg)
93
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 94: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/94.jpg)
94
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 95: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/95.jpg)
95
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
y=6 y=Undefined gives what?
![Page 96: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/96.jpg)
96
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 97: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/97.jpg)
97
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 98: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/98.jpg)
98
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 99: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/99.jpg)
99
Global constant propagation
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 100: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/100.jpg)
100
Global constant propagation
exit x = 4;Undefined
x=y=w=6z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 101: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/101.jpg)
101
Global constant propagation
exit x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 102: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/102.jpg)
102
Global constant propagation
exit x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 103: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/103.jpg)
103
Global constant propagation
exitx=y=w=z=6x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 104: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/104.jpg)
104
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 105: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/105.jpg)
105
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 106: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/106.jpg)
106
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 107: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/107.jpg)
107
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 108: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/108.jpg)
108
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 109: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/109.jpg)
109
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
x=6 x=4 gives what?
![Page 110: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/110.jpg)
110
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 111: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/111.jpg)
111
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 112: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/112.jpg)
112
Global constant propagation
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 113: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/113.jpg)
113
Global constant propagation
exity=w=6 x = 4;x=4, y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 114: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/114.jpg)
114
Global constant propagation
exity=w=6 x = 4;x=4, y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Global analysisreached fixpoint
![Page 115: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/115.jpg)
115
Global constant propagation
exity=w=6x = 4;y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 116: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/116.jpg)
116
Global constant propagation
exity=w=6x = 4;y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = 6;x=y=w=6
x=6y = 6;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
![Page 117: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/117.jpg)
117
Dataflow for constant propagation
• Direction: Forward• Semilattice: Vars {Undefined, 0, 1, -1, 2, -2, …, Not-
a-Constant}– Join mapping for variables point-wise
{x1,y1,z1} {x1,y2,zNot-a-Constant} = {x1,yNot-a-Constant,zNot-a-Constant}
• Transfer functions:– fx=k(V) = V|xk (update V by mapping x to k)
– fx=a+b(V) = V|xNot-a-Constant (assign Not-a-Constant)
• Initial value: x is Undefined– (When might we use some other value?)
![Page 118: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/118.jpg)
118
Proving termination
• Our algorithm for running these analyses continuously loops until no changes are detected
• Given this, how do we know the analyses will eventually terminate?– In general, we don‘t
![Page 119: Compiler Principles Winter 2012-2013 Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.](https://reader035.fdocuments.in/reader035/viewer/2022070400/56649f135503460f94c27c2c/html5/thumbnails/119.jpg)
See you next time
119