
















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
Material Type: Notes; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Local optimizations
Consider the expression: a + a * ( b - c ) + ( b - c ) * d
Tree Directed acyclic graph
a *
a -
b c
b c
d
a -
b c
d
Local optimizations
Common subexpressions (CSE)
Directed acyclic graph (DAG)
Building a DAG for an expression
Directed acyclic graph example
Code After Renaming a = b + c a 0 = b 0 + c 0 b = a - d b 1 = a 0 - d 0 c = b + c c 1 = b 1 + c 0 d = a - d d 1 = a 0 - d 0
b 0 c 0
d 0
c 1
b 1 ,d 1
a 0
Common subexpressions
Going beyond basic blocks
Examples
c = a+b if (...) a = ... d = a+b
if (...) c = a+b d = a+b
We handle these conditions using data-flow analysis
Data-flow analysis
Algorithm
Example control flow graph
a = 1 if (b) then c = a+b else b = 1 c = a+b ...
Available expressions
Definition
Global common subexpression elimination
Available expressions example
Node KILL GEN A a+b ∅ B ∅ a+b C a+b a+b D ∅ ∅
AVAIL(B) = GEN(A) ∪ (AVAIL(A) – KILL(A)) = ∅ ∪ (∅ – { a+b }) = ∅ AVAIL(C) = GEN(A) ∪ (AVAIL(A) – KILL(A)) = ∅ ∪ (∅ – { a+b }) = ∅ AVAIL(D) = (GEN(B) ∪ (AVAIL(B) – KILL(B))) ∩ (GEN(C) ∪ (AVAIL(C) – KILL(C))) = ({ a+b } ∪ (∅ – ∅)) ∩ ({ a+b } ∪ (∅ – { a+b })) = { a+b }
Solving data-flow equations
Iterative algorithm
change = true; while (change) change = false; for each basic block // faster in reverse PostOrder: solve data-flow equations for b if (old 6 = new) change = true; end for end while
Speed of solution
Reaching definitions
Constant propagation:
a = 1 a = 2 a = 2 b = 3 = a = b
Loop invariant code motion:
L: a = a + 4 b = 20 c = b + a if (...) goto L
Reaching definitions
REACH(b) =
⋃ x∈pred(b)
(DEF(x) ∪ (REACH(x) − KILL(x)))
Best case for REACH(b) = ∅
Worse case for REACH(b) = { all definitions }
Live variables
LIVE(b) =
⋃ x∈succ(b)
(USE(x) ∪ (LIVE(x) − KILL(x)))
Best case for LIVE(b) = ∅
Worse case for LIVE(b) = { all definitions }
What do these have in common?
AVAIL(b) =
⋂ x∈pred(b)
(GEN(x) ∪ (AVAIL(x) − KILL(x)))
REACH(b) =
⋃ x∈pred(b)
(DEF(x) ∪ (REACH(x) − KILL(x)))
LIVE(b) =
⋃ x∈succ(b)
(USE(x) ∪ (LIVE(x) − KILL(x)))
General equations:
IN(b) = ∧p∈pred(b) OUT(p)† OUT(b) = GEN(b) ∪ (IN(b) - KILL(b))
† Reverse graph for backward problem.
Data-flow lattices
Definitions
Data-flow lattices
Available expressions example: let D = { x | x ⊆ {e 1 , e 2 , e 3 }}, ∧ = ∩
Partial ordering {e 1 , e 2 } vs. {e 3 }
Single lattice vs. one for each variable