Optimization PDF
Optimization PDF
0-0
Peephole optimization
MOV R0 a
MOV a R0 ← delete if in same B.B.
2. Algebraic simplification: eliminate instructions like the
following:
x = x + 0 x = x * 1
3. Strength reduction: replace expensive operations by
equivalent cheaper operations, e.g.
x2 → x * x
fixed-point multiplication/division by power of 2 → shift
4. Jumps:
goto L1 if a < b goto L1
... ...
L1: goto L2 L1: goto L2
⇓ ⇓
goto L2 if a < b goto L2
... ...
L1: goto L2 L1: goto L2
eliminate
i = m-1; j = n; v = a[n];
while (1) {
do i = i+1; while (a[i] < v);
do j = j-1; while (a[j] > v);
if (i >= j) break;
x = a[i]; a[i] = a[j]; a[j] = x;
Section 10.1
}
x = a[i]; a[i] = a[n]; a[n] = x;
1 i = m-1 16 t7 = 4*i
2 j = n 17 t8 = 4*j
3 t1 = 4*n 18 t9 = a[t8]
4 v = a[t1] 19 a[t7] = t9
5 i = i+1 20 t10 = 4*j
6 t2 = 4*i 21 a[t10] = x
7 t3 = a[t2] 22 goto 5
8 if t3 < v goto 5 23 t11 = 4*i
9 j = j-1 24 x = a[t11]
10 t4 = 4*j 25 t12 = 4*i
11 t5 = a[t4] 26 t13 = 4*n
12 if t5 > v goto 9 27 t14 = a[t13]
13 if i>=j goto 23 28 a[t12] = t14
14 t6 = 4*i 29 t15 = 4*n
15 x = a[t6] 30 a[t15] = x
Compiler Construction: Code Optimization – p. 5/35
Basic blocks
1 i = m-1 16 t7 = 4*i
2 j = n 17 t8 = 4*j
3 t1 = 4*n 18 t9 = a[t8]
4 v = a[t1] 19 a[t7] = t9
5 i = i+1 20 t10 = 4*j
6 t2 = 4*i 21 a[t10] = x
7 t3 = a[t2] 22 goto 5
8 if t3 < v goto 5 23 t11 = 4*i
9 j = j-1 24 x = a[t11]
10 t4 = 4*j 25 t12 = 4*i
11 t5 = a[t4] 26 t13 = 4*n
12 if t5 > v goto 9 27 t14 = a[t13]
13 if i>=j goto 23 28 a[t12] = t14
14 t6 = 4*i 29 t15 = 4*n
15 x = a[t6] 30 a[t15] = x
Compiler Construction: Code Optimization – p. 6/35
Flow graph
i = m-1 t6 = 4*i
j = n x = a[t6]
B1
t1 = 4*n t7 = 4*i
v = a[t1] t8 = 4*j
t9 = a[t8] B5
i = i+1 a[t7] = t9
t2 = 4*i t10 = 4*j
B2
t3 = a[t2] a[t10] = x
if t3 < v goto B2 goto B2
t11 = 4*i
x = a[t11]
j = j-1
t12 = 4*i
t4 = 4*j
B3 t13 = 4*n
t5 = a[t4] B6
t14 = a[t13]
if t5 > v goto B3
a[t12] = t14
t15 = 4*n
B4 if i>=j goto B6 a[t15] = x
Compiler Construction: Code Optimization – p. 7/35
Optimization methods
Plan:
Structured programs
Section 10.5
reaching definitions
Flow graphs / Iterative solutions
reaching definitions
available expressions
live variables
gen(S) = {d}
d: a=b+c kill (S) = Da − {d}
in(S1 ) = in(S)
S2 in(S2 ) = out(S1 )
out(S) = out(S2 )
gen(S) = gen(S1 )
kill (S) = kill (S1 )
S1
Reaching definitions
Input: flow graph with gen , kill sets computed for each BB
Output: in , out sets for each block
Method:
P1 P2 ... Pn
Section 10.6
S1 S2 ... Sn
2. While changes occur:
for each block BS
out(B) ← S in(S) where S - successor of B
in(B) ← use(B) ∪ (out (B) − def (B))
edge consists of d along with all nodes from which we can reach
n without going through d
Header: the node that dominates all other nodes in a loop
Pre-header: Given a loop L with header h:
1. create an empty block p;
2. make h the only successor of p;
3. all edges which entered h from outside L are changed to
point to p (edges to h from inside L are not changed).
B1 i=1
B2 if u<v goto B3
B3
i=2 The block containing s must
u=u+1 dominate all exit nodes of the
loop (i.e. nodes with a succes-
sor not in the loop).
v=v-1
B4
if v<20 goto B5
B5 j=i
B1 i=1
B2
i=3
if u<v goto B3
B3 There is no other assignment to
i=2
x in the loop.
u=u+1
B5 j=i
B1 i=1
B2
if u<v goto B3
B5 j=i