global optimization
TRANSCRIPT
![Page 1: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/1.jpg)
Global Optimization
![Page 2: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/2.jpg)
2
Lecture Outline
•
Global flow analysis
•
Global constant propagation
•
Liveness analysis
![Page 3: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/3.jpg)
3
Local Optimization
Recall the simple basic-block optimizations–
Constant propagation
–
Dead code elimination
x := 42
y := z * w
q := y + x
x := 42
y := z * w
q := y + 42
y := z * w
q := y + 42
![Page 4: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/4.jpg)
4
Global Optimization
These optimizations can be extended to an entire control-flow graph
x := 42
b > 0
y := z * w y := 0
q := y + x
![Page 5: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/5.jpg)
5
Global Optimization
These optimizations can be extended to an entire control-flow graph
x := 42
b > 0
y := z * w y := 0
q := y + x
![Page 6: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/6.jpg)
6
Global Optimization
These optimizations can be extended to an entire control-flow graph
x := 42
b > 0
y := z * w y := 0
q := y + 42
![Page 7: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/7.jpg)
7
Correctness
•
How do we know whether it is OK to globally propagate constants?
•
There are situations where it is incorrect:
x := 42
b > 0
y := z * w
x := 54
y := 0
q := y + x
![Page 8: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/8.jpg)
8
Correctness (Cont.)
To replace a use of x
by a constant k we must know that the following property ** holds:
On every path to the use of x, the last assignment to x is x := k **
![Page 9: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/9.jpg)
9
Example 1 Revisited
x := 42
b > 0
y := z * w y := 0
q := y + x
![Page 10: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/10.jpg)
10
Example 2 Revisited
x := 42
b > 0
y := z * w
x := 54
y := 0
q := y + x
![Page 11: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/11.jpg)
11
Discussion
•
The correctness condition is not trivial to check
•
“All paths”
includes paths around loops and through branches of conditionals
•
Checking the condition requires global analysis–
An analysis that determines how data flows over the entire control-flow graph of a function/method
![Page 12: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/12.jpg)
12
Global Analysis
Global optimization tasks share several traits:–
The optimization depends on knowing a property P at a particular point in program execution
–
Proving P
at any point requires knowledge of the entire function body
–
Property P
is typically undecidable
!–
It is OK to be conservative: If the optimization requires P
to be true, then want to know either
•
that P
is definitely true, or•
that we don’t know whether P
is true
–
It is always safe to say “don’t know”•
We try to say do not know as rarely as possible
![Page 13: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/13.jpg)
13
Global Analysis (Cont.)
•
Global dataflow analysis is a standard technique for solving problems with these characteristics
•
Global constant propagation is one example of an optimization that requires global dataflow analysis
![Page 14: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/14.jpg)
14
Global Constant Propagation
•
On every path to the use of x, the last assignment to x is x := k **
•
Global constant propagation can be performed at any point where property **
holds
•
Consider the case of computing **
for a single variable x
at all program points
![Page 15: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/15.jpg)
15
Global Constant Propagation (Cont.)
•
To make the problem precise, we associate one of the following values with x
at every
program point
Don’t know whether
x
is a constant*
x
= constant ccThis statement never executes#
interpretationvalue
![Page 16: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/16.jpg)
16
Example
x = *x = 42
x = 42
x = 42x = 54
x = *
x := 42
b > 0
y := z * w
x := 54
y := 0
q := y + x
x = 42
x = 42
x = *
![Page 17: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/17.jpg)
17
Using the Information
•
Given global constant information, it is easy to perform the optimization–
Simply inspect the x = ?
associated with a
statement using x–
If x
is constant at that point replace that use of x
by the constant
•
But how do we compute the properties x = ?
![Page 18: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/18.jpg)
18
The Analysis Idea
The analysis of a (complicated) program can be expressed as a combination of simple rules relating the change in information between
adjacent statements
![Page 19: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/19.jpg)
19
Explanation
•
The idea is to “push”
or “transfer”
information from one statement to the next
•
For each statement s, we compute information about the value of x
immediately before and
after sCin
(x,s)
= value of x
before sCout
(x,s)
= value of x
after s
![Page 20: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/20.jpg)
20
Transfer Functions
•
Define a transfer function
that transfers information from one statement to another
•
In the following rules, let statement s
have as immediate predecessors statements p1
,…,pn
![Page 21: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/21.jpg)
21
Rule 1
if Cout
(x, pi
) = *
for any i, then Cin
(x, s) = *
s
x = *
x = *
x = ?x = ?x = ?
![Page 22: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/22.jpg)
22
Rule 2
If
Cout
(x, pi
) = c and
Cout
(x, pj
) = d and
d ≠
c then Cin
(x, s) = *
s
x = d
x = *
x = ?x = ?x = c
![Page 23: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/23.jpg)
23
Rule 3
if Cout
(x, pi
) = c or # for all i,then Cin
(x, s) = c
s
x = c
x = c
x = #x = # x = c
![Page 24: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/24.jpg)
24
Rule 4
if Cout
(x, pi
) =
# for all i,then Cin
(x, s) = #
s
x = #
x = #
x = #x = # x = #
![Page 25: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/25.jpg)
25
The Other Half
•
Rules 1-4 relate the out of one statement to the in of the successor statement–
they propagate information forward
across CFG
edges
•
We also need rules relating the in of a statement to the out
of the same statement
–
to propagate information across statements
![Page 26: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/26.jpg)
26
Rule 5
Cout
(x, s) =
# if Cin
(x, s) = #
sx = #
x = #
![Page 27: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/27.jpg)
27
Rule 6
Cout
(x, x := c) =
c if c
is a constant
x := cx = ?
x = c
![Page 28: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/28.jpg)
28
Rule 7
Cout
(x, x := f(…)) =
*
x := f(…)x = ?
x = *
This rule says that we do not perform inter-procedural analysis (i.e. we do not look at what other functions do)
where f
is a function other than the one being analyzed
![Page 29: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/29.jpg)
29
Rule 8
Cout
(x, y := …) =
Cin
(x, y := …) if
x ≠
y
y := . . .x = a
x = a
![Page 30: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/30.jpg)
30
An Algorithm
1.
For every entry s
to the function, set Cin
(x, s) = *
2.
Set Cin
(x, s) = Cout
(x, s) = #
everywhere else
3.
Repeat until all points satisfy 1-8:Pick s
not satisfying 1-8 and update using the
appropriate rule
![Page 31: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/31.jpg)
31
The Value #
To understand why we need #, look at a loop
x := 42
b > 0
y := z * w y := 0
q := y + x
q < b
x = *x = 42
x = 42
x = 42
x = 42
![Page 32: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/32.jpg)
32
Discussion
•
Consider the statement y := 0•
To compute whether x
is constant at this
point, we need to know whether x
is constant at the two predecessors–
x := 42
–
q := y + x
•
But information for q := y + x
depends on its predecessors, including y := 0!
![Page 33: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/33.jpg)
33
The Value # (Cont.)
•
Because of cycles, all points must have values at all times
•
Intuitively, assigning some initial value allows the analysis to break cycles
•
The initial value # means “So far as we know, control never reaches this point”
![Page 34: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/34.jpg)
34
Example
x := 42
b > 0
y := z * w y := 0
q := x + y
q < b
x = *x = 42
x = 42
x = 42
x = 42
x = #
x = #
x = #
![Page 35: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/35.jpg)
35
Example
x := 42
b > 0
y := z * w y := 0
q := x + y
q < b
x = *x = 42
x = 42
x = 42
x = 42
x = #
x = #
x = 42
![Page 36: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/36.jpg)
36
Example
x := 42
b > 0
y := z * w y := 0
q := x + y
q < b
x = *x = 42
x = 42
x = 42
x = 42
x = #
x = 42
x = 42
![Page 37: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/37.jpg)
37
Example
x := 42
b > 0
y := z * w y := 0
q := x + y
q < b
x = *x = 42
x = 42
x = 42
x = 42
x = 42
x = 42
x = 42
![Page 38: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/38.jpg)
38
Orderings
•
We can simplify the presentation of the analysis by ordering the values
# <
c <
*
•
Drawing a picture with “lower”
values drawn lower, we get
#
*
-1 0 1 ......
![Page 39: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/39.jpg)
39
Orderings (Cont.)
•
*
is the greatest value, #
is the least–
All constants are in between and incomparable
•
Let lub be the least-upper bound in this ordering
•
Rules 1-4 can be written using lub:Cin(x, s) = lub { Cout(x, p) | p is a predecessor of s }
![Page 40: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/40.jpg)
40
Termination
•
Simply saying “repeat until nothing changes” doesn’t guarantee that eventually we reach a
point where nothing changes
•
The use of lub
explains why the algorithm terminates–
Values start as #
and only increase
–
#
can change to a constant, and a constant to *–
Thus, C_(x, s)
can change at most twice
![Page 41: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/41.jpg)
41
Termination (Cont.)
Thus the algorithm is linear in program size
Number of steps = // worst caseNumber of C_(….)
values computed * 2 =
Number of program statements * 4
![Page 42: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/42.jpg)
42
Liveness Analysis
Once constants have been globally propagated, we would like to eliminate dead code
After constant propagation, x := 42 is dead (assuming x is not used elsewhere)
x := 42
b > 0
y := z * w y := 0
q := y + x
![Page 43: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/43.jpg)
43
Live and Dead Variables
•
The first value of x
is dead (never used)
•
The second value of x is live (may be used)
•
Liveness is an important concept for the compiler
x := 17
x := 42
y := x
![Page 44: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/44.jpg)
44
Liveness
A variable
x
is live at statement s
if–
There exists a statement s’
that uses x
–
There is a path from s
to s’
–
That path has no intervening assignment to x
![Page 45: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/45.jpg)
45
Global Dead Code Elimination
•
A statement x := …
is dead code if x
is dead after the assignment
•
Dead statements can be deleted from the program
•
But we need liveness information first . . .
![Page 46: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/46.jpg)
46
Computing Liveness
•
We can express liveness in terms of information transferred between adjacent statements, just as in copy propagation
•
Liveness is simpler than constant propagation, since it is a boolean
property (true or false)
![Page 47: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/47.jpg)
47
Liveness Rule 1
Lout
(x, p) = ∨
{ Lin
(x, s) | s a successor of
p }
p
x = true
x = true
x = ?x = ?x = ?
![Page 48: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/48.jpg)
48
Liveness Rule 2
Lin
(x, s) =
true if s
refers to x
on the RHS
…:= f(x)x = true
x = ?
![Page 49: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/49.jpg)
49
Liveness Rule 3
Lin
(x, x := e) =
false if e does not refer to
x
x := ex = false
x = ?
![Page 50: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/50.jpg)
50
Liveness Rule 4
Lin
(x, s) =
Lout
(x, s)
if s
does not refer to
x
sx = a
x = a
![Page 51: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/51.jpg)
51
Algorithm
1.
Let all L_(…) = false
initially
2.
Repeat until all statements s
satisfy rules 1-4Pick s
where one of 1-4 does not hold and
update using the appropriate rule
![Page 52: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/52.jpg)
52
Termination
•
A value can change from false
to true, but not the other way around
•
Each value can change only once, so termination is guaranteed
•
Once the analysis information is computed, it is simple to eliminate dead code
![Page 53: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/53.jpg)
53
Forward vs. Backward Analysis
We have seen two kinds of analysis:
•
An analysis that enables constant propagation:–
this is a forwards analysis: information is pushed from inputs to outputs
•
An analysis that calculates variable liveness:–
this is a backwards analysis: information is pushed from outputs back towards inputs
![Page 54: Global Optimization](https://reader031.vdocument.in/reader031/viewer/2022020623/61f27817478f873cad7310f7/html5/thumbnails/54.jpg)
54
Global Flow Analyses
•
There are many other global flow analyses
•
Most can be classified as either forward or backward
•
Most also follow the methodology of local rules relating information between adjacent program points