Code-Optimization
Code-Optimization
1
Introduction
• Most of the programs spend 90% of their execution time in 10% of the
code
• Program’s inner loops are good candidates for improvement.
2
Code Optimization
• Code Optimization refers to the techniques a compiler can employ in
an attempt to produce better object language program.
• The quality of an object program is generally measured by its size or
its run time.
• It is theoretically impossible for a compiler to produce the best
possible object program for every source program under any
reasonable cost function.
• So the more accurate term for code optimization would be “code
improvement”
3
The aspects of code optimization
4
Principal Sources of Optimization
5
Criteria for Code-Improving
Transformation / Code Optimization
REQUIREMENTS:
– 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
6
Places for potential improvements
7
Peephole Optimizations
8
Characteristics of Peephole Optimization
• Algebraic Simplification
9
Peephole Optimizations
MOV R0 , a
Constant Folding
x := 32 becomes x := 64
x := x + 32
10
Peephole Optimizations
Unreachable Code
goto L2
x := x + 1 No need if unlabeled
11
Peephole Optimizations
Algebraic Simplification
x := x + 0 No needed
x := x * 1 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
12
13
14
15
16
Principles Sources of Optimization
18
Common Expression can be Eliminated
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
19
Now, suppose i is a constant:
i=4
t2 = b[5]
Final Code:
a[5] = t2
20
Common Expression can be Eliminated
21
22
23
24
Dead-Code Elimination
Code Motion
Move invariants out of the loop.
Example:
while (i <= limit - 2)
becomes
t := limit - 2
while (i <= t)
Blocks
1) 1st stmt
2) Target of conditional or unconditional go to
3) Stmt after conditional or unconditional go to
26
27
Optimizations on CFG
– Constant Propagation
– …
B4
if i >= j goto B6
32
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i
B4
if i >= j goto B6
33
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i
B4
if i >= j goto B6
34
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 *i
B4
if i >= j goto B6
35
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i
B4
if i >= j goto B6
36
B1 Common Subexpression Elimination
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
B2 t6 = 4 * i t11 = 4 * i
B4
if i >= j goto B6
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
B1 Reduction in Strength
i=m-1
j=n
t1 =4 * n
v = a[t1]
B5 B6
t2 = 4 * i a[t2] = t5
B2 t14 = a[t1]
t4 = 4 * j
a[t4] = t3 a[t2] = t14
goto B2 a[t1] = t3
t2 = t 2 + 4
t3 = a[t2]
B3 if t3 < v goto B2
t4 = t4 - 4
t5 = a[t4]
if t5 > v goto B3
B4
if i >= j goto B6
50
The End
51