Code Optimization
Code Optimization
Compiler Optimization
– Local/peephole optimizations
– Recognizing loops
– Dataflow analysis
Outline (cont’d)
Optimizing (cont’d)
– Alias analysis
Code generation
– Instruction selection
– Register allocation
Flow graph
2. For each leader its basic block consists of the leader and all
statements up to but not including the next leader
Partitioning into basic blocks (cont’d)
1 prod=0
BB1
2 i=1
3 t1=4*i
4 t2=a[t1]
5 t3=4*i
6 t4=b[t3]
BB2 7 t5=t2*t4
8 t6=prod+t5
9 prod=t6
10 t7=i+i
11 i=t7
12 if i < 21 goto 3
Intermediate Representation (cont’d)
C-like
Like ASTs:
Nodes can have variable names attached that contain the value of
that expression
1. op
2. op
3.
DAG example
+ t6, prod
1 t1 = 4 * i
2 t2 = a[t1] prod * t5
3 t3 = 4 * i
[] t2 [] t4 <=
4 t4 = b[t3]
5 t5 = t2 * t4 t1, t3 + t7, i
6 t6 = prod + t5 * 20
7 prod = t6
a b 4 i 1
8 t7 = i + 1
9 i = t7
10 if ( ) goto 1
Local optimizations
Examples
Function-preserving transformations
– Constant folding
– Copy propagation
– Dead-code elimination
Algebraic transformations
Constant folding
Copy propagation
Dead-code elimination
If each statement that defines a temporary defines a new temporary,
then the basic block is in normal-form
Machine dependent eliminations/transformations
3 $Lx: ...
Use of machine idioms, e.g.,
– SIMD instructions
Global optimizations
Loop optimizations
Code motion
while ( )
while ( )
Loop optimizations (cont’d)
Variables that are locked to the iteration of the loop are called
induction variables
Example: in for (i = 0; i < 10; i++) is an induction
variable
Loops can contain more than one induction variable, for example,
hidden in an array lookup computation
Strength reduction
Its use is not limited to loops but can be helpful for induction variable
elimination
Loop optimizations (cont’d)
Note that in the previous strength reduction we have to initialize
before the loop
if ( ) goto top
if ( ) goto top
Algorithms for global optimizations
available do the following
– Create a new variable
– Replace statements by
– Replace statement by
Copy propagation
may now substitute a use of by a use of if
– Statement is the only definition of reaching the use
assignments to
Copy propagation (cont’d)
To find the set of copy statements we can use, we define a new
dataflow problem
Dataflow equations:
– Determine the uses of reached by this definition of
reaching it ( )
Detection of loop-invariant computations
(a) it is in a block that dominate all exits of the loop
i = 1 B1
i = 1 B1 i = 1 B1
i=3
if u < v goto B3 B2 if u < v goto B3 B2 if u < v goto B3 B2
v=v−1 v=v−1
B4 B4 k=i
if v <= 20 goto B5 if v <= 20 goto B5 v=v−1 B4
if v <= 20 goto B5
j = i B5 j = i B5
j = i B5
A basic induction variable is a variable that only has assignments
of the form
Associated with each induction variable is a triple where
is a basic induction variable and and are constants such that
In this case belongs to the family of
The basic induction variable belongs to its own family, with the
associated triple
Detection of induction variables (cont’d)
Find variables with a single assignment in the loop with one of the
following forms:
– , , , , , where
is a constant and is an induction variable
If is not basic and in the family of then there must be
– No assignment of between the assignment of and
Consider each basic induction variable in turn. For each variable
in the family of with triple :
– Create a new variable
– Replace the assignment to by
– Immediately after each assignment append
– Place in the family of with triple
– Initialize in the preheader:
Strength reduction for induction variables (cont’d)
B1 B1
i=m−1 i=m−1
t1 = 4 * n t1 = 4 * n
v = a[t1] v = a[t1]
s2 = 4 * i
B2
i=i+1 Strength reduction B2
t2 = 4 * i i=i+1
t3 = a[t2] s2 = s2 + 4
if t3 < v goto B2 t2 = s2
t3 = a[t2]
B3 if t3 < v goto B2
if i < n goto B5
B3
if i < n goto B5
B4 B5
B4 B5
Elimination of induction variables
Consider each basic induction variable only used to compute other
induction variables and tests
Take some in ’s family such that and from the triple
are simple
if ( relop )
Do some copy propagation to eliminate assignments formed
during strength reduction