Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- http//www.eecg.toronto.edu/voss/ece540/
- January 31, 2002
2Corrections and Clarifications
3Value Numbering Algorithm
Initialize AVAIL set to empty for each
instruction (res expr) in BB if
(instruction is of form res rhs ) rv
value number of rhs // give new value if
necessary associate res with rv
else if (instruction is of form res lhs op rhs)
lv value number of lhs // give
new value number if necessary rv value
number of rhs // give new value number if
necessary if ( (lv op rv) Î AVAIL )
v value number of (lv op rv)
replace (lhs op rhs) by a variable with value
number v associate with res the value
number v else v new value
number associate with res the value
number v associate with (lv op rv) the
value number v add (lv op rv) to AVAIL
else if (instruction is of form res
op rhs ) possibly cleanup AVAIL set
(remove elements that contain value
numbers
that are no longer active).
4Value Numbering Example
g x y h u v i x y i u v x u
v x k u g h u x y w g h
5Irreducible CFG
- BB1 dom BB3
- entry at BB2
- not natural
- BB2 dom BB4
- natural loop
- rare in real apps
BB1
BB2
BB3
BB4
Reducible! Since for all retreating edges h dom
t. BB3 -gt BB1 is therefore natural.
6Finding Back Edges
- Perform depth-first traversal of CFG (N,E).
- A retreating edge e (t,h) Î E is a back edge if
h dom t. - A retreating edge is always a back edge in a loop
written using structured constructs (i.e., no
gotos). - The CFGs of such programs are said to be
reducible graphs. - One can use gotos to obtain irreducible flow
graphs. - We will only consider reducible CFGs , so we can
just look for a retreating edge e (t,h) Î E.
7Algorithm for Finding the Nodes in the Loop
Stack S Empty Set Loop h insert_on_stack
(t) while (S is not empty) m pop (S)
for each pred(m)
insert_on_stack (p) insert_on_stack
(a) if (a Ï Loop) loop loop U
a push_on_stack (a)
8Natural Loops and Reducible Graphs
- If all retreating edges are back edges (h dom t),
the graph is reducible - if all back edges are removed, a directed acyclic
graph remains where all nodes can be reached from
Entry. - back edges define natural loops according to the
given algorithm.
9Redundancy Optimizations
10Some well known redundancy optimizations
- Value numbering
- Common subexpression elimination
- Forward substitution (reverse of CSE)
- Copy propagation
- Loop-invariant code motion
- Code Hoisting
11Common Subexpression Elimination
- An occurrence of an expression is a common
subexpression if there is another occurrence that
always precedes it in execution order and whose
operands remain unchanged between these
evaluations. - i.e. the expression has been already computed and
the result is still valid. - Common Subexpression Elimination replaces the
recomputations with a saved value. - reduces the number of computations
12CSE Algorithm
Perform Available Expressions analysis for each b
Î N for each instruction Î b in the form z
x op y such that x op y is available at
entry of b 1. determine if x op y
is available at instruction 2.
determine evaluations of x op y that reach z
3. create a new variable t 4.
replace definitions w x op y found in step 2.
with t x op y
w t 5. replace z x op y with z
t
13AE - Data flow Equations
- AEin(b) Set of available expression at the
beginning of b. - AEout(b) Set of available that reach the end
of b.
14CSE Example 1
Entry
c ab d ac e dd i 1
BB 1
f ab c c2 c gt d?
BB 2
d c g dd
BB 4
BB 3
g ac
i i1 i gt 10?
BB 5
Exit
15CSE Example 2
Entry
c ab d ac e dd i 1
BB 1
f ab c c2 c gt d?
BB 2
d c g dd
BB 4
BB 3
g ac
i i1 i gt 10?
BB 5
Exit
16CSE Example 3
Entry
c ab d ac e dd i 1
BB 1
f ab c c2 c gt d?
BB 2
d c g dd
BB 4
BB 3
g dd
i i1 i gt 10?
BB 5
Exit
17CSE Example 4
Entry
c ab d ac e dd i 1
BB 1
f ab c c2 c gt d?
BB 2
d c g dd
BB 4
BB 3
g dd
i dd i gt 10?
BB 5
Exit
18CSE Limitations
- Expressions must be textually identical
- sort operands
- global value numbering (see Muchnick)
19Forward Substitution
- Replace a copy by reevaluation of the expression
- Why?
- perhaps holds a register too long, causes spills
- See that you have a store of an expression to a
temporary followed by an assignment to a
variable. If the expression operands are not
changed to point of substitution replace with
expression.
t1 b 2 a t1
a b 2
c b 2 d a b
c t1 d a b
20Copy Propagation
- A copy instruction is an instruction in the form
x y. - Copy propagation replaces later uses of x with
uses of y provided intervening instructions do
not change the value of either x or y. - Benefit saves computations, reduces space
enables other transformations.
21Copy Propagation (cont)
- To propagate a copy statement in the form s x
y, we must - Determine all places where this definition of x
is used. - For each such use, u
- s must be the only definition of x reaching u
and - on every path from s to u, there are no
assignments to y.
22Available Copy Expressions
- Copy expressions available at the input of basic
blocks can be computed using a data-flow analysis
similar to available expressions.
- Copy(b) Set of copy instructions u v in b
such that u and v are not assigned to later in
b (i.e. copy instructions available at end of
b). - Kill(b) Set of copy instructions that are
killed by b. That is, the set of copy
instructions in other blocks that have one or
more of their operands are assigned to in
b. - CopyIn(b) Set of copy instructions available at
the beginning of b. - CopyOut(b) Set of copy instructions available
at the end of b.
23Copy Propagation - Example
24Copy Propagation - Example
- the copy expression is the only definition that
reaches the statement - the copy expression is still valid, i.e. its RHS
has not been modified.
25Copy Propagation - Example
- the copy expression is the only definition that
reaches the statement - the copy expression is still valid, i.e. its RHS
has not been modified.
after dead-code elimination
26Loop-Invariant Code Motion
- A computation inside a loop is said to be
loop-invariant if its execution produces the same
value as long as control stays within the loop. - Loop-invariant code motion moves such
computations outside the loop (into the loop
pre-header). - Benefit eliminates redundant computations.
27Loop Invariants
- Appear mostly (but not always) because of array
references in loops.
for (i0iltni) for (j0jltmj)
aij 10nj
for (i0iltni) for (j0jltmj)
t1 im t2 t1 j t3
10n (a t2) t3j
t3 10n for (i0iltni) t1 im
for (j0jltmj) t2 t1 j
(a t2) t3j
28Loop Invariants
- An operation in a BB that belongs to a loop is
loop-invariant if for each operand of the
operation - the operand is constant, or
- all definitions that reach the operand are
outside the loop.
29Loop Invariants (Contd)
- An operation in a BB that belongs to a loop is
also loop-invariant if for each operand of the
operation - there is exactly one definition that reaches the
operand and this definition is inside the loop
and is itself loop-invariant.Why exactly one?
30Loop Invariants (Contd)
- An iterative algorithm is used to determine
loop-invariants - 1. Mark invariant instructions for which each
operand - is constant, or
- has all its reaching definitions outside loop.
- 2. Repeat until no more instructions are marked
loop invariant Mark invariant each instruction
not previously marked, whose - operands are all marked as loop-invariants, or
- operands have exactly one reaching definition
from inside the loop, and that definition is in
an instruction which is marked invariant.
31Loop-Invariants - Example
Entry
b 2 i 1
i gt 100
a b 1 c 2 i2 0
d a d e 1 d
d -c f 1 a
i i 1 a lt 2
Exit
32Loop-Invariant Code Motion Algorithm
Perform Reaching Definitions analysis Build
ud-chains Determine Loop-Invariants in loop
L for each loop-invariant instruction s that
defines v
move loop-invariant
instruction to Ls pre-hearder
if ( 1. BB s dominates all exits of L
2. v is not defined elsewhere in L
3. all uses of v in L can only be
reached by the definition of v in
s )
33Code Motion - Condition 1
34Code Motion - Condition 2
35Code Motion - Condition 3
the instruction defining a variable v must be in
a BB the dominates all uses of v in the loop.
36Loop-Invariants - Example
Entry
b 2 i 1
i gt 100
a b 1 c 2 i2 0
d a d e 1 d
d -c f 1 a
i i 1 a lt 2
Exit
37Entry
b 2 i 1
i gt 100
a b 1 c 2 i2 0
d a d e 1 d
d -c f 1 a
i i 1 a lt 2
Exit
38Code Motion - BB Examining Order
y y d
a b 1 c 2 d2 0
d y d y 1 d
f 1 a y gt 20
d i 1 x a
- Breadth-first traversal visits siblings before
children. - Process the BBs in breadth first order.
- Move loop invariants into pre-header in the order
they are discovered!
39Zero-Iteration Loops
- Move loop-invariant into pre-header and protect
with a guard that tests whether a loop is entered
or not. - Invert the loop.
40Loop Invariants (Contd)
- An operation in a BB that belongs to a loop is
also loop-invariant if for each operand of the
operation - there is exactly one definition that reaches the
operand and this definition is inside the loop
and is itself loop-invariant.Why exactly one?
41Example
42Code Hoisting
- Code Hoisting finds expressions that are always
evaluated following a point p and moves them to
the latest point beyond which they are always
evaluated. - reduces space occupied by the program
- may impact execution time positively, negatively
or not at all - Uses Very Busy Expressions
- VBEin(b) Set of very busy expressions at the
beginning of b. - VBEout(b) Set of very busy expressions at the
end of b.
43Code Hoisting Algorithm
for each BB b for each exp in VBEout(b)
for each BB x dominated by b
replace 1st reachable exp in x with tmp
if replacement not yet inserted
insert tmp exp at end of b
44Code Hoisting Example
ENTRY
cd
a lt 10
cd
cd
ab,ac,cd
e c d d 2
f a c c d gt 0
ab,ac
?
ab,ac
ab,ac,ad,cd
i a b j a d
g a b h a c
?
ab,ac,cd
EXIT
45Code Hoisting Example
ENTRY
cd
a lt 10
cd
cd
ab,ac,cd
e c d d 2
f a c c d gt 0
?
ab,ac
ab,ac
ab,ac,ad,cd
i a b j a d
g a b h a c
?
ab,ac,cd
EXIT