Download Lecture Notes on Local Optimizations - Introduction to Compilers | CMSC 430 and more Study notes Computer Science in PDF only on Docsity!
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)
- portion of expressions
- repeated multiple times
- computes same value
- can reuse previously computed value
Directed acyclic graph (DAG)
- program representation
- nodes can have multiple parents
- no cycles allowed
- exposes common subexpressions
Building a DAG for an expression
- maintain hash table for leafs, expressions
- unique name for each node — its value number
- reuse nodes found in hash table
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
- can no longer build DAGs
- must consider control flow
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
- build control flow graph (CFG)
- initial (local) data gathering
- propagate information around the graph
- post-processing (if needed)
Example control flow graph
a = 1 if (b) then c = a+b else b = 1 c = a+b ...
Available expressions
Definition
- An expression is defined at point p if its value is computed at p.
- An expression is killed at a point p if one of its argument variables is defined at p.
- an expression e is available at a point p in a procedure if every path leading to p contains a prior definition of e that is not killed between its definition and p.
Global common subexpression elimination
- If, at some definition point for p = e, e is available with name x, we can replace the evaluation with a reference to x.
- requires a global naming scheme
- natural analog to parts of value numbering
Available expressions example
Node KILL GEN A a+b ∅ B ∅ a+b C a+b a+b D ∅ ∅
AVAIL(A) = ∅
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
- node may change only if some predecessor changes
- try to visit node after all its predecessors
- reverse PostOrder propagates information quickly
- programs usually converge after 3–4 passes
- use bitvectors for more efficiency
Reaching definitions
- The problem: What are the assignments (or definitions) of a variable x that may reach a particular reference to x?
- Why is this useful?
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
- A definition of a variable x is a statement that assigns, or may assign, a value to x.
- A definition d reaches a program point p if there exists a path from the point immediately following d to p such that d is not killed along that path.
- REACH(b) is the set of definitions reaching the entry of basic block b
- DEF(b) is the set of local definitions in b that reach the end of b
- KILL(b) is the set of variables killed by b
- Equations:
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
- Slightly different, since information at basic block is based on what happens later in the program.
- A backward data-flow problem.
- LIVE(b) is the set of definitions live on exit from block b.
- KILL(b) is as before.
- USE(b) is the set of locally exposed uses
- succ(b) is the set of basic blocks that are immediate successors of b in the control flow graph.
- Equations:
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)))
- Confluence Operator or Meet Function: union or intersection
- Behavior for block: GEN and KILL
- A direction: forward (confluence over predecessors) or backward (over successors)
- Best case set value: >
- Worst case set value: ⊥
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
- a lattice is a set L and a meet operation ∧ such that, ∀ a, b, c ∈ L (a) a ∧ a = a [idempotent] (b) a ∧ b = b ∧ a [commutative] (c) a ∧ (b ∧ c) = (a ∧ b) ∧ c [associative]
- ∧ imposes a partial order on L, ∀ a, b ∈ L (a) a ≥ b ⇔ a ∧ b = b (b) a > b ⇔ a ≥ b and a 6 = b
- a lattice may have a bottom element ⊥ (a) ∀ a ∈ L, ⊥ ∧ a = ⊥ (b) ∀ a ∈ L, a ≥ ⊥
- a lattice may have a top element > (a) ∀ a ∈ L, > ∧ a = a (b) ∀ a ∈ L, > ≥ a
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