0% found this document useful (0 votes)
4 views

Code-Optimization

Code optimization involves program transformations to enhance execution speed and reduce space usage, focusing on frequently executed code segments. Techniques include local and global optimizations, with a focus on correctness, average speedup, and cost-effectiveness. Peephole optimizations and function-preserving transformations are key methods to eliminate redundancy and improve efficiency without altering program functionality.

Uploaded by

rb4466184
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code-Optimization

Code optimization involves program transformations to enhance execution speed and reduce space usage, focusing on frequently executed code segments. Techniques include local and global optimizations, with a focus on correctness, average speedup, and cost-effectiveness. Peephole optimizations and function-preserving transformations are key methods to eliminate redundancy and improve efficiency without altering program functionality.

Uploaded by

rb4466184
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 51

Code Optimization

1
Introduction

Code Optimization ( here only machine independent/ local


optimization)
• program transformations to make the program to run faster, take less
space or both.
• Find Frequently executed part, make it efficient as possible.

• 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

 The primary question is how beneficial the given optimization is


and how it costs
 In some situation quick and straight forward translation is
sufficient

4
Principal Sources of Optimization

• Code optimization techniques are generally applied after syntax


analysis, usually both after and before code generation.
• The techniques consist of detecting patterns in the program and
replacing these patterns by equivalent but more efficient
constructs.
• The richest source of optimization is the efficient utilization of
the registers and instruction set of the machine.

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

Source Intermediate Target


program Front Code Code Program
|
|
End | Generator |
|
| | |
|
User Can Compiler Can
Compiler Can
Profile program Use registers
Improve loops
Change Select instructions
Procedure calls
Algorithm Do Peephole
Address
Transform loops transformations
Calculations

7
Peephole Optimizations

• A Simple but effective technique for locally improving the


target code is peephole optimization,

• A method for trying to improve the performance of the


target program by examining a short sequence of target
instructions( peephole) and replacing these instructions by a
shorter or faster sequence whenever possible.

8
Characteristics of Peephole Optimization

• Redundant instruction elimination

• Flow of control information

• Algebraic Simplification

• Use of machine Idioms ( machine instructions auto


inc/dec , i=i+1)

9
Peephole Optimizations

Redundant Instruction Elimination

MOV R0 , a

MOV a, R0  No need if unlabeled

Constant Folding

x := 32 becomes x := 64

x := x + 32

10
Peephole Optimizations

Unreachable Code

goto L2

x := x + 1  No need if unlabeled

Flow of control optimizations

goto L1 becomes goto L2

L1: goto L2 No needed if no other JUMP to L1 branch

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

• A transformation of a program is called local if it can be


performed by looking only at the statements in a basic
block.
• Otherwise, it is called global.

• Many transformations can be performed at both the local


and global levels.
• Local transformations are usually faster.
17
Function-Preserving Transformations
• There are a number of ways in which a compiler can improve a
program without changing the function it computes.
 Common-subexpression elimination,
 Copy propagation,
 Dead-code elimination,
Are common examples of such function-preserving
transformations; called semantics-preserving transformations.

18
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

19
Now, suppose i is a constant:

i=4 i=4 i=4


t1 = i+1 t1 = 5(i cons t1 = 5
t2 = b[t1] so t1 cons)
t2 = b[5]
a[t1] = t2 t2 = b[t1]
a[5] = t2
a[t1] = t2

i=4
t2 = b[5]
Final Code:
a[5] = t2

20
Common Expression can be Eliminated

/* Sum neighbors of i,j */ int inj = i*n + j;


up = val[(i-1)*n + j]; up = val[inj - n];
down = val[(i+1)*n + j]; down = val[inj + n];
left = val[i*n + j-1]; left = val[inj - 1];
right = val[i*n + j+1]; right = val[inj + 1];
sum = up + down + left + right; sum = up + down + left + right;

21
22
23
24
Dead-Code Elimination

• A variable is live at a point in a program if its value


can be used subsequently, otherwise it is dead at that
point.
• Dead or useless code is such statements that compute
values never get used.
• If ( debug ) print ………..
• By data-flow analysis, it may be possible to deduce
that each time the program reaches this statement, the
value of debug is false.
• debug := false
• If copy propagation replaces debug by false, then the
print statement is dead because it cannot be reached.
25
Simple Loop Optimizations

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

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.
28
Three Address Code of Quick Sort
1 i=m-1 16 t7 = 4 * I
2 j=n 17 t8 = 4 * j each array
3 t1 =4 * n 18 t9 = a[t8]
takes 4
bytes
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
11 26 t12 = 4 * i
t5 = a[t4]
12 27 t13 = 4 * n
if t5 > v goto (9)
13 28 t14 = a[t13]
14 if i >= j goto (23) 29 a[t12] = t14
15 t6 = 4 * i 30 t15 = 4 * n 29
x = a[t6]
a[t ] = x
Find The Leaders
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
11 26 t12 = 4 * i
t5 = a[t4]
12 27 t13 = 4 * n
if t5 > v goto (9)
13 28 t14 = a[t13]
14 if i >= j goto (23) 29 a[t12] = t14
15 t6 = 4 * i 30 t15 = 4 * n 30
x = a[t6]
a[t ] = x
Find The Basic Block
B1 i=m-1 t7 = 4 * I
j=n t8 = 4 * j
t1 =4 * n t9 = a[t8]
v = a[t1] a[t7] = t9
B2 i=i +1 t10 = 4 * j
t2 = 4 * i
a[t10] = x
t3 = a[t2]
goto (5)
B6
if t3 < v goto (5) t11 = 4 * i
B3 j=j–1 x = a[t11]
t4 = 4 * j
t12 = 4 * i
t5 = a[t4]
t13 = 4 * n
if t5 > v goto (9)
B4 t14 = a[t13]
if i >= j goto (23)
B5 a[t12] = t14
t6 = 4 * i
t15 = 4 * n 31
x = a[t6]
a[t ] = 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
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

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

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

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

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

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

You might also like