Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- Redundancy Optimizations, Feb 1, 2005
2Some well known redundancy optimizations
- Value numbering (Local Global)
- Common subexpression elimination
- Forward substitution (reverse of CSE)
- Copy propagation
- Loop-invariant code motion
- Code Hoisting
3Common 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
4CSE 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
5AE - 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.
6CSE 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
7CSE 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
8CSE 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
9CSE 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
10Alternative to finding the evaluations that reach
the use
- Assign a particular variable v to hold each
expression - For each use that is not reached by an available
expression - Add a v exp immediately prior to the use
- Replace the use with v
- For each use reached by an avail expr
- Replace the use with v
11CSE Limitations
- Expressions must be textually identical
- sort operands
- global value numbering (see Muchnick)
12Forward 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
13Copy 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.
14Copy 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.
15Available 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.
16Copy Propagation - Example
DEF-USE
USE-DEF
17Copy 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.
18Copy 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
19Loop-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.
20Loop 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
21Loop 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.
22Loop 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?
23Loop 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
- have exactly one reaching definition from inside
the loop, and that definition is in an
instruction which is marked invariant.
24Loop-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
25Loop-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 )
26Code Motion - Condition 1
27Code Motion - Condition 2
28Code Motion - Condition 3
the instruction defining a variable v must be in
a BB the dominates all uses of v in the loop.
29Loop-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
30Entry
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
31Code 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!
32Zero-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.
33Code 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.
34Code 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
35Code 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
36Code 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