![Page 1: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/1.jpg)
Global Optimization
Lecture 21
![Page 2: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/2.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
2
Lecture Outline
• Global flow analysis
• Global constant propagation
• Liveness analysis
![Page 3: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/3.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
3
Local Optimization
Recall the simple basic-block optimizations – Constant propagation – Dead code elimination
X := 3
Y := Z * W
Q := X + Y
X := 3
Y := Z * W
Q := 3 + Y
Y := Z * W
Q := 3 + Y
![Page 4: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/4.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
4
Global Optimization
These optimizations can be extended to an entire control-flow graph
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
![Page 5: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/5.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
5
Global Optimization
These optimizations can be extended to an entire control-flow graph
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
![Page 6: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/6.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
6
Global Optimization
These optimizations can be extended to an entire control-flow graph
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * 3
![Page 7: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/7.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
7
Correctness
• How do we know it is OK to globally propagate constants?
• There are situations where it is incorrect: X := 3
B > 0
Y := Z + W
X := 4
Y := 0
A := 2 * X
![Page 8: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/8.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
8
Correctness (Cont.)
To replace a use of x by a constant k we must know that:
On every path to the use of x, the last assignment to x is x := k (Invariant #1)
![Page 9: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/9.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
9
Example 1 Revisited
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
![Page 10: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/10.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
10
Example 2 Revisited
X := 3
B > 0
Y := Z + W
X := 4
Y := 0
A := 2 * X
![Page 11: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/11.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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 of the entire control-flow graph
![Page 12: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/12.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
12
Global Analysis
Global optimization tasks share several traits: – The optimization depends on knowing a property X
at a particular point in program execution – Proving X at any point requires knowledge of the
entire program – It is OK to be conservative. If the optimization
requires X to be true, then want to know either • X is definitely true • Don’t know if X is true
– It is always safe to say “don’t know”
![Page 13: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/13.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/14.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
14
Global Constant Propagation
• Global constant propagation can be performed at any point where Invariant #1 holds
• Consider the case of computing Invariant #1 for a single variable X at all program points
![Page 15: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/15.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
15
Global Constant Propagation (Cont.)
• To make the problem precise, we associate one of the following values with X at every program point
value interpretation
z X=z means that analysis hasn’t determined if control reaches that point
c X = constant c
top X is definitely not a constant
![Page 16: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/16.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
16
Example
X = top X = 3
X = 3
X = 3 X = 4
X = top
X := 3
B > 0
Y := Z + W
X := 4
Y := 0
A := 2 * X
X = 3
X = 3
X = top
![Page 17: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/17.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/18.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
18
The 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: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/19.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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 s
C(x,s,in) = value of x before s C(x,s,out) = value of x after s
![Page 20: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/20.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
20
Transfer Functions
• Define a transfer function that transfers information one statement to another
• In the following rules, let statement s have immediate predecessor statements p1,…,pn
![Page 21: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/21.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
21
Rule 1
if C(pi, x, out) = top for any i, then C(s, x, in) = top
s
X = top
X = top
X = ? X = ? X = ?
![Page 22: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/22.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
22
Rule 2
C(pi, x, out) = c & C(pj, x, out) = d & d <> c then
C(s, x, in) = top
s
X = d
X = top
X = ? X = ? X = c
![Page 23: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/23.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
23
Rule 3
if C(pi, x, out) = c or z for all i, then C(s, x, in) = c
s
X = c
X = c
X = z X = z X = c
![Page 24: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/24.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
24
Rule 4
if C(pi, x, out) = z for all i, then C(s, x, in) = z
s
X = z
X = z
X = z X = z X = z
![Page 25: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/25.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
25
The Other Half
• Rules 1-4 relate the out of one statement to the in of the next statement
• Now we need rules relating the in of a statement to the out of the same statement
![Page 26: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/26.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
26
Rule 5
C(s, x, out) = z if C(s, x, in) = z
s X = z
X = z
![Page 27: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/27.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
27
Rule 6
C(x := c, x, out) = c if c is a constant
x := c X = ?
X = c
![Page 28: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/28.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
28
Rule 7
C(x := f(…), x, out) = top
x := f(…) X = ?
X = top
![Page 29: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/29.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
29
Rule 8
C(y := …, x, out) = C(y := …, x, in) if x <> y
y := . . . X = a
X = a
![Page 30: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/30.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
30
An Algorithm
1. For every entry s to the program, set C(s, x, in) = top
2. Set C(s, x, in) = C(s, x, out) = z everywhere else
3. Repeat until all points satisfy 1-8: Pick s not satisfying 1-8 and update using the
appropriate rule
![Page 31: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/31.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
31
The Value z • To understand why we need z, look at a loop
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
A < B
X = top X = 3
X = 3
X = 3
X = 3
![Page 32: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/32.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
32
Discussion
• Consider the statement Y := 0 • To compute whether X is constant after this
statement, we need to know whether X is constant at the two predecessors – X := 3 – A := 2 * X
• But info for A := 2 * X depends on its predecessors, including Y := 0!
![Page 33: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/33.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
33
The Value z (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 z means “So far as we know, control never reaches this point”
![Page 34: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/34.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
34
Example
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
A < B
X = top X = 3
X = 3
X = 3
X = 3
X = z
X = z X = z
![Page 35: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/35.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
35
Example
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
A < B
X = top X = 3
X = 3
X = 3
X = 3
X = z
X = z
X = 3
![Page 36: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/36.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
36
Example
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
A < B
X = top X = 3
X = 3
X = 3
X = 3
X = z
X = 3
X = 3
![Page 37: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/37.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
37
Example
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
A < B
X = top
X = 3
X = 3
X = 3
X = 3
X = 3
X = 3
X = 3
![Page 38: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/38.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
38
Orderings
• We can simplify the presentation of the analysis by ordering the values
z < c < top
• Drawing a picture with “lower” values drawn lower, we get
z
top
-1 0 1
![Page 39: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/39.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
39
Orderings (Cont.)
• top is the greatest value, z 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: C(s, x, in) = lub { C(p, x, out) | p is a predecessor of s }
![Page 40: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/40.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
40
How do we argue that this algo terminates?
• Simply saying “repeat until nothing changes” doesn’t guarantee that eventually nothing changes
• The use of lub explains why the algorithm terminates – Values start as z and only increase z can change to a constant, and a constant to top – Thus, C(s, x, _) can change at most twice
![Page 41: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/41.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
41
Termination (Cont.)
Thus the algorithm is linear in program size Number of steps = Number of C(….) value computed * 2 = Number of program statements * 4
![Page 42: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/42.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
42
Liveness Analysis
Once constants have been globally propagated, we would like to eliminate dead code
After constant propagation, X := 3 is dead (assuming X not used elsewhere)
X := 3
B > 0
Y := Z + W Y := 0
A := 2 * X
![Page 43: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/43.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
43
New Example: Live and Dead
• The first value of x is dead (never used)
• The second value of x is live (may be used)
• Liveness is an important concept
X := 3
X := 4
Y := X
![Page 44: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/44.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
44
Liveness
A variable x is live at statement s if
– There exists a statement s’ that uses x such that
• There is a path from s to s’
• That path has no intervening assignment to x
![Page 45: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/45.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/46.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/47.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
47
Liveness Rule 1
L(p, x, out) = ∨ { L(s, x, in) | s a successor of p }
p
X = true
X = true
X = ? X = ? X = ?
![Page 48: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/48.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
48
Liveness Rule 2
L(s, x, in) = true if s refers to x on the rhs
…:= f(x) X = true
X = ?
![Page 49: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/49.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
49
Liveness Rule 3
L(x := e, x, in) = false if e does not refer to x
x := e X = false
X = ?
![Page 50: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/50.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
50
Liveness Rule 4
L(s, x, in) = L(s, x, out) if s does not refer to x
s X = a
X = a
![Page 51: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/51.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
51
Algorithm
1. Let all L(…) = false initially
2. Repeat until all statements s satisfy rules 1-4 Pick s where one of 1-4 does not hold and update
using the appropriate rule
![Page 52: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/52.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
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 is computed, it is simple to eliminate dead code
![Page 53: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/53.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
53
Forward vs. Backward Analysis
We’ve seen two kinds of analysis: Constant propagation is a forwards analysis:
information is pushed from inputs to outputs Liveness is a backwards analysis: information is
pushed from outputs back towards inputs
![Page 54: Lecture 21 - University of Waterloovganesh/TEACHING/W2014/lectures/lecture21.pdfProf. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh) 12 Global Analysis Global optimization tasks](https://reader031.vdocument.in/reader031/viewer/2022040808/5e4c635a1929f31aef1e1941/html5/thumbnails/54.jpg)
Prof. Alex Aiken Lecture (Modified by Prof. Vijay Ganesh)
54
Analysis
• 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