0% found this document useful (0 votes)
169 views30 pages

Compiler Ch10

The document discusses code optimization techniques including peephole optimizations, constant folding, dead code elimination, common subexpression elimination, and loop optimizations like code motion to improve performance by examining and transforming short sequences of instructions or expressions within basic blocks and across the control flow graph. Peephole optimizations make local improvements by replacing instructions with faster equivalents while other optimizations like common subexpression elimination aim to remove redundant computations.

Uploaded by

api-3712520
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views30 pages

Compiler Ch10

The document discusses code optimization techniques including peephole optimizations, constant folding, dead code elimination, common subexpression elimination, and loop optimizations like code motion to improve performance by examining and transforming short sequences of instructions or expressions within basic blocks and across the control flow graph. Peephole optimizations make local improvements by replacing instructions with faster equivalents while other optimizations like common subexpression elimination aim to remove redundant computations.

Uploaded by

api-3712520
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Code Optimization

Dewan Tanvir Ahmed


Assistant Professor, CSE
BUET
Introduction

Criteria for Code-Improving Transformation:


– Meaning must be preserved (correctness)
– Speedup must occur on average.
– Work done must be worth the effort.

Opportunities:
– Programmer (algorithm, directives)
– Intermediate code
– Target code
Peephole Optimizations (9.9)
1. A Simple but effective technique for locally improving the target code is
peephole optimization,
2. a method for trying to improve the performance of the target program
3. by examining a short sequence of target instructions and replacing these
instructions by a shorter or faster sequence whenever possible.

Characteristics of peephole optimization


1. Redundant instruction elimination
2. Flow of control information
3. Algebraic Simplification
4. Use of machine Idioms
Peephole Optimizations

Constant Folding
x := 32 becomes x := 64
x := x + 32
Unreachable Code
goto L2
x := x + 1  No need
Flow of control optimizations
goto L1 becomes goto L2

L1: goto L2  No needed if no other L1 branch
Peephole Optimizations

Algebraic Simplification
x := x + 0  No needed
Dead code
x := 32  where x not used after statement
y := x + y  y := y + 32
Reduction in strength
x := x * 2  x := x + x
 x := x << 2
Basic Block Level

1. Common Sub expression elimination


2. Constant Propagation
3. Copy Propagation
4. Dead code elimination
5. …
Common expression can be eliminated

Simple example: a[i+1] = b[i+1]

t1 = i+1 t1 = i + 1
t2 = b[t1] t2 = b[t1]
t3 = i + 1 t3 = i + 1  no longer live
a[t3] = t2 a[t1] = t2
Now, suppose i is a constant:

i=4 i=4 i=4


t1 = i+1 t1 = 5 t1 = 5
t2 = b[t1] t2 = b[t1] t2 = b[5]
a[t1] = t2 a[t1] = t2 a[5] = t2

i=4
Final Code: t2 = b[5]
a[5] = t2
Optimizations on CFG
Must take control flow into account
– Common Sub-expression Elimination
– Constant Propagation
– Dead Code Elimination
– Partial redundancy Elimination
– …
Applying one optimization may raise opportunities for other
optimizations.
Simple Loop Optimizations

Code Motion
Move invariants out of the loop.
Example:
while (i <= limit - 2)
becomes
t := limit - 2
while (i <= t)
Three Address Code of Quick Sort
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
Find The Basic Block
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
B1 Flow Graph
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t7 = 4 * i t12 = 4 * i
t3 = a[t2] t8 = 4 * j t13 = 4 * n
if t3 < v goto B2 t9 = a[t8] t14 = a[t13]
B3 a[t7] = t9 a[t12] = t14
j=j–1 t10 = 4 * j t15 = 4 * n
t4 = 4 * j
a[t10] = x a[t15] = x
t5 = a[t4]
goto B2
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t7 = 4 * i t12 = 4 * i
t3 = a[t2] t8 = 4 * j t13 = 4 * n
if t3 < v goto B2 t9 = a[t8] t14 = a[t13]
B3 a[t7] = t9 a[t12] = t14
j=j–1 t10 = 4 * j t15 = 4 * n
t4 = 4 * j
a[t10] = x a[t15] = x
t5 = a[t4]
goto B2
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 t10 = 4 * j a[t12] = t14
j=j–1 a[t10] = x t15 = 4 * n
t4 = 4 * j
goto B2 a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 *i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 a[t8] = x a[t12] = t14
j=j–1 goto B2 t15 = 4 * n
t4 = 4 * j
a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t8 = 4 * j t12 = 4 * i
t3 = a[t2] t9 = a[t8] t13 = 4 * n
if t3 < v goto B2 a[t6] = t9 t14 = a[t13]
B3 a[t8] = x a[t12] = t14
j=j–1 goto B2 t15 = 4 * n
t4 = 4 * j
a[t15] = x
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i

i=i +1 x = a[t6] x = a[t11]


t2 = 4 * i t8 = 4 * j t13 = 4 * n
t3 = a[t2] t9 = a[t8] t14 = a[t13]
if t3 < v goto B2 a[t6] = t9 a[t11] = t14
B3 a[t8] = x t15 = 4 * n
j=j–1 goto B2 a[t15] = x
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i
t11 = 4 * i
i=i +1 x = a[t6]
x = a[t11]
t2 = 4 * i t8 = 4 * j
t13 = 4 * n
t3 = a[t2] t9 = a[t8]
t14 = a[t13]
if t3 < v goto B2 a[t6] = t9
a[t11] = t14
B3 a[t8] = x
a[t13] = x
j=j–1 goto B2
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i
t11 = 4 * i
i=i +1 x = a[t6]
x = a[t11]
t2 = 4 * i t8 = 4 * j
t13 = 4 * n
t3 = a[t2] t9 = a[t8]
t14 = a[t13]
if t3 < v goto B2 a[t6] = t9
a[t11] = t14
B3 a[t8] = x
a[t13] = x
j=j–1 goto B2
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = a[t2]
t11 = 4 * i
i=i +1 t8 = 4 * j
x = a[t11]
t2 = 4 * i t9 = a[t8]
t13 = 4 * n
t3 = a[t2] a[t2] = t9
t14 = a[t13]
if t3 < v goto B2
a[t8] = x
a[t11] = t14
B3 goto B2
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i
i=i +1 t8 = 4 * j
x = a[t11]
t2 = 4 * i t9 = a[t8]
t13 = 4 * n
t3 = a[t2] a[t2] = t9
t14 = a[t13]
if t3 < v goto B2
a[t8] = x
a[t11] = t14
B3 goto B2
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i
i=i +1 t9 = a[t4]
x = a[t11]
t2 = 4 * i a[t2] = t9
t13 = 4 * n
t3 = a[t2] a[t4] = x
t14 = a[t13]
if t3 < v goto B2
goto B2
a[t11] = t14
B3
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = t3
t11 = 4 * i
i=i +1 a[t2] = t5
x = a[t11]
t2 = 4 * i a[t4] = x
t13 = 4 * n
t3 = a[t2] goto B2
t14 = a[t13]
if t3 < v goto B2
a[t11] = t14
B3
a[t13] = x
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = t3
x = t3
i=i +1 a[t2] = t5
t14 = a[t1]
t2 = 4 * i a[t4] = x
a[t2] = t14
t3 = a[t2] goto B2
a[t1] = x
if t3 < v goto B2

B3
j=j–1 Similarly for B6
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Dead Code Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 x = t3
x = t3
i=i +1 a[t2] = t5
t14 = a[t1]
t2 = 4 * i a[t4] = x
a[t2] = t14
t3 = a[t2] goto B2
a[t1] = x
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Dead Code Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 a[t2] = t5 t14 = a[t1]
a[t4] = t3 a[t2] = t14
i=i +1
t2 = 4 * i goto B2 a[t1] = t3
t3 = a[t2]
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Reduction in Strength
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 a[t2] = t5 t14 = a[t1]
a[t4] = t3 a[t2] = t14
i=i +1
t2 = 4 * i goto B2 a[t1] = t3
t3 = a[t2]
if t3 < v goto B2

B3
j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
B1 Reduction in Strength
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
t2 = 4 * i
B2 a[t2] = t5 t14 = a[t1]
t4 = 4 * j
a[t4] = t3 a[t2] = t14
goto B2 a[t1] = t3
t2 = t 2 + 4
t3 = a[t2]
if t3 < v goto B2
B3

t4 = t 4 - 4
t5 = a[t4]
if t5 > v goto B3

B4
if i >= j goto B6
The End

You might also like