





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note from ecs 142 - compiler design course by prof. Su. It covers the topic of global optimization, specifically global dataflow analysis and constant propagation. The lecture outlines the concepts of global dataflow analysis, two example dataflow analyses - global constant propagation and liveness analysis, and the importance of correctness in global optimization.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Prof. Su ECS 142 Lecture 16 1
Global Optimization
ECS 142
Prof. Su ECS 142 Lecture 16 2
Lecture Outline
Prof. Su ECS 142 Lecture 16 3
Local Optimization
Recall the simple basic-block optimizations
X := 3 Y := Z * W Q := X + Y
X := 3 Y := Z * W Q := 3 + Y
Y := Z * W Q := 3 + Y
Prof. Su ECS 142 Lecture 16 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
Prof. Su ECS 142 Lecture 16 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
Prof. Su ECS 142 Lecture 16 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
Prof. Su ECS 142 Lecture 16 7
Correctness
X := 3 B > 0
Y := Z + W X := 4
Y := 0
A := 2 * X Prof. Su ECS 142 Lecture 16 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 **
Prof. Su ECS 142 Lecture 16 9
Example 1 Revisited
X := 3 B > 0
Y := Z + W Y := 0
A := 2 * X
Prof. Su ECS 142 Lecture 16 10
Example 2 Revisited
X := 3 B > 0
Y := Z + W X := 4
Y := 0
A := 2 * X
Prof. Su ECS 142 Lecture 16 11
Discussion
Prof. Su ECS 142 Lecture 16 12
Global Analysis
Global optimization tasks share several traits:
Prof. Su ECS 142 Lecture 16 19
Explanation
Prof. Su ECS 142 Lecture 16 20
Transfer Functions
Prof. Su ECS 142 Lecture 16 21
Rule 1
if C (^) out(x, pi) = * for some i, then C (^) in(x, s) = *
s
X = *
X = *
X =? X =? X =?
Prof. Su ECS 142 Lecture 16 22
Rule 2
If C (^) out(x, pi) = c and C (^) out(x, p (^) j) = d and d ≠ c then C (^) in (x, s) = *
s
X = d
X = *
X = c X =? X =?
Prof. Su ECS 142 Lecture 16 23
Rule 3
if C (^) out(x, pi) = c or # for all i, then C (^) in(x, s) = c
s
X = c
X = c
X = c X = # X = #
Prof. Su ECS 142 Lecture 16 24
Rule 4
if C (^) out(x, pi) = # for all i, then C (^) in(x, s) = #
s
X = #
X = #
X = # X = # X = #
Prof. Su ECS 142 Lecture 16 25
The Other Half
Prof. Su ECS 142 Lecture 16 26
Rule 5
C (^) out(x, s) = # if C (^) in(x, s) = #
s
X = #
X = #
Prof. Su ECS 142 Lecture 16 27
Rule 6
C (^) out(x, x := c) = c if c is a constant
x := c
X =?
X = c
Prof. Su ECS 142 Lecture 16 28
Rule 7
C (^) out(x, x := f(…)) = *
x := f(…)
X =?
X = *
Prof. Su ECS 142 Lecture 16 29
Rule 8
C (^) out(x, y := …) = C (^) in(x, y := …) if x ≠ y
y :=...
X = a
X = a
Prof. Su ECS 142 Lecture 16 30
An Algorithm
Prof. Su ECS 142 Lecture 16 37
Example
X := 3 B > 0
Y := Z + W Y := 0
A := 2 * X A < B
X = * X = 3
X = 3
X = 3
X = 3
X = 3 X = 3
X = 3
Prof. Su ECS 142 Lecture 16 38
Orderings
… -1 0 1 …
Prof. Su ECS 142 Lecture 16 39
Orderings (Cont.)
Prof. Su ECS 142 Lecture 16 40
Termination
Prof. Su ECS 142 Lecture 16 41
Termination (Cont.)
Thus the algorithm is linear in program size
Number of steps =
Number of C_(….) values computed * 2 =
Number of program statements * 4
Prof. Su ECS 142 Lecture 16 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
Prof. Su ECS 142 Lecture 16 43
Live and Dead
X := 3
X := 4
Y := X
Prof. Su ECS 142 Lecture 16 44
Liveness
A variable x is live at statement s if
Prof. Su ECS 142 Lecture 16 45
Global Dead Code Elimination
Prof. Su ECS 142 Lecture 16 46
Computing Liveness
Prof. Su ECS 142 Lecture 16 47
Liveness Rule 1
Lout(x, p) = ∨ { Lin(x, s) | s a successor of p }
p
X = true
X = true
X =? X =? X =?
Prof. Su ECS 142 Lecture 16 48
Liveness Rule 2
Lin(x, s) = true if s refers to x on the rhs
…:= f(x)
X = true
X =?