Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Course : Comp6062 - Compilation

Techniques
Effective Period : September 2018

Code Optimization

Session 23 - 24
Learning Outcomes

At the end of this meeting, expected student


will be able to:

• Students can explain the various methods


of code optimization (C2)

• Students can use the method of code


optimization in a particular case (C3)
2
Content Outline

• Definition of code optimization

• The principles of source code optimization

• Optimization per block

• Optimization loop

• Data flow analysis

• Basic blocks and flow graph

3
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

4
Peephole Optimizations

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
5
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

6
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

7
Basic Block Level

1. Common Sub expression elimination


2. Constant Propagation
3. Copy Propagation
4. Dead code elimination
5. Code Motion

8
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

9
Now, suppose i is a constant:
• i=4 • i=4 i=4
• t1 = i+1 • t1 = 5 t1 = 5
• t2 = • t2 = b[t1] t2 = b[5]
b[t1] • a[t1] = t2 a[5] = t2
• a[t1] =
t2

Final Code: i=4


t2 = b[5]
a[5] = t2

10
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.

11
Simple Loop Optimizations

• Code Motion
Move invariants out of the loop.
Example:
while (i <= limit - 2)
becomes
t := limit - 2
while (i <= t)

12
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
6 t10 = 4 * j
t2 = 4 * i 21
7 a[t10] = x
t3 = a[t2] 22
8 goto (5)
if t3 < v goto (5) 23
9 t11 = 4 * I
j=j–1 24
10 25 x = a[t11]
t4 = 4 * j
11 26 t12 = 4 * i
t5 = a[t4]
12 27 t13 = 4 * n
13 if t5 > v goto (9)
28 t14 = a[t13]
14 if i >= j goto (23)
29 a[t12] = t14
15 t6 = 4 * i
30 t15 = 4 * n
x = a[t6] 13
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 23
if t3 < v goto (5) t11 = 4 * i
9 24
j=j–1 x = a[t11]
10 25
t4 = 4 * j t12 = 4 * i
11 26
t5 = a[t4] t13 = 4 * n
12 27
13 if t5 > v goto (9) 28 t14 = a[t13]
14 if i >= j goto (23) 29 a[t12] = t14
15 t6 = 4 * i 14 30 t15 = 4 * n
x = a[t ]
Flow Graph
B1 i=m-1
j=n
t1 =4 * n B5 B6
v = a[t1] t6 = 4 * i t11 = 4 * i
x = a[t6] x = a[t11]
B2 i=i +1 t7 = 4 * i t12 = 4 * i
t2 = 4 * i
t8 = 4 * j t13 = 4 * n
t3 = a[t2]
t9 = a[t8] t14 = a[t13]
if t3 < v goto B2
a[t7] = t9 a[t12] = t14
t10 = 4 * j t15 = 4 * n
B3 j=j–1
a[t10] = x a[t15] = x
t4 = 4 * j
t5 = a[t4] goto B2

if t5 > v goto B3
B4
if i >= j goto B6
15
Common Subexpression Elimination

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

B4 if i >= j goto B6
16
Common Subexpression Elimination

B1 i=m-1
j=n
t1 =4 * n
B5 t6 = 4 * i t11 = 4 * i
v = a[t1] B6
x = a[t6] x = a[t11]
i=i +1 t8 = 4 * j
B2 t12 = 4 * i
t2 = 4 * i t9 = a[t8] t13 = 4 * n
t3 = a[t2] a[t6] = t9 t14 = a[t13]
if t3 < v goto B2
t10 = 4 * j a[t12] = t14
a[t10] = x t15 = 4 * n
B3 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
17
Common Subexpression Elimination

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

B4 if i >= j goto B6
18
Common Subexpression Elimination

i=m-1
B1 j=n
t1 =4 * n B6
v = a[t1] B5 t6 = 4 * i t11 = 4 * i
x = a[t6] x = a[t11]
i=i +1
B2 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]
a[t8] = x a[t12] = t14
B3 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
19
Common Subexpression Elimination

i=m-1
B1 j=n B6
t1 =4 * n
t6 = 4 * i t11 = 4 * i
v = a[t1] B5
x = a[t6] x = a[t11]

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

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

B4 if i >= j goto B6
20
Common Subexpression Elimination

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

i=m-1
B1 j=n
B6
t1 =4 * n
v = a[t1] B5 t6 = 4 * i t11 = 4 * i

x = a[t6] x = a[t11]

B2 t8 = 4 * j t13 = 4 * n
i=i +1
t2 = 4 * i t9 = a[t8] t14 = a[t13]
t3 = a[t2] a[t6] = t9 a[t11] = t14
if t3 < v goto B2 a[t13] = x
a[t8] = x

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

B4
if i >= j goto B6 22
Common Subexpression Elimination

i=m-1
B1 j=n
t1 =4 * n
B5 B6
v = a[t1] x = a[t2]
t11 = 4 * i
i=i +1 t8 = 4 * j
x = a[t11]
t2 = 4 * i t9 = a[t8]
t13 = 4 * n
B2 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
23
Common Subexpression Elimination

B1 i=m-1
j=n
t1 =4 * n
B5 B6
x = t3
v = a[t1] t11 = 4 * i
t8 = 4 * j
x = a[t11]
i=i +1 t9 = a[t8]
t13 = 4 * n
B2 t2 = 4 * i a[t2] = t9
t14 = a[t13]
t3 = a[t2]
a[t8] = x
if t3 < v goto B2 a[t11] = t14
goto B2
a[t13] = x

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

B4 if i >= j goto B6
24
Common Subexpression Elimination

B1 i=m-1
j=n B6
t1 =4 * n x = t3
B5 t11 = 4 * i
v = a[t1]
t9 = a[t4]
x = a[t11]
a[t2] = t9
i=i +1 t13 = 4 * n
B2 t2 = 4 * i a[t4] = x
t14 = a[t13]
t3 = a[t2] goto B2
a[t11] = t14
if t3 < v goto B2
a[t13] = x

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

B4 if i >= j goto B6
25
Common Subexpression Elimination

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

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

B4 if i >= j goto B6
26
Common Subexpression Elimination

i=m-1
j=n
B1
t1 =4 * n B6
B5 x = t3
v = a[t1] x = t3
a[t2] = t5
t14 = a[t1]
a[t4] = x
i=i +1 a[t2] = t14
B2 t2 = 4 * i goto B2
a[t1] = x
t3 = a[t2]
if t3 < v goto B2
Similarly for B6
B3 j=j–1
t4 = 4 * j
t5 = a[t4]
if t5 > v goto B3

B4 if i >= j goto B6
27
Dead Code Elimination

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

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

B4 if i >= j goto B6
28
Dead Code Elimination

i=m-1
B1
j=n B6
t1 =4 * n
B5 a[t2] = t5 t14 = a[t1]
v = a[t1]
a[t4] = t3 a[t2] = t14

goto B2 a[t1] = t3
i=i +1
B2 t2 = 4 * i
t3 = a[t2]
if t3 < v goto B2

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

B4 if i >= j goto B6
29
Reduction in Strength

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

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

B4 if i >= j goto B6
30
Reduction in Strength

i=m-1
B1 j=n B6
t1 =4 * n B5 a[t2] = t5 t14 = a[t1]
v = a[t1]
a[t4] = t3 a[t2] = t14
t2 = 4 * i
goto B2 a[t1] = t3
t4 = 4 * j

t2 = t 2 + 4
B2
t3 = a[t2]
if t3 < v goto B2

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

B4 if i >= j goto B6
31
References
• Aho, A.V., Ravi, S., & Ullman, J.D. (2007). Compiler :
Principle, techniques and tools. 2nd. Addison-Wesley.
New York. ISBN : 0321491696, Chapter 8.1 – 8.4 (page
505-531)
• http://xbean.cs.ccu.edu.tw/~software/GraduateSlides/c
hapter10.ppt
• http://dragonbook.stanford.edu/lecture-
notes/Stanford-CS143/20-Optimization.pdf

Bina Nusantara University 32

You might also like