0% found this document useful (0 votes)
25 views3 pages

Chapter 8 - Code Optimization Part 2

Uploaded by

Aschalew Ayele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Chapter 8 - Code Optimization Part 2

Uploaded by

Aschalew Ayele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Compiler Design

Chapter Eight: Code Optimization


Objective,
❖ Explain code optimization, the process as well as the goal of code optimization.
❖ Describe code optimization techniques: Dead code elimination, Loop optimizations, Induction
variables and Reduction in strength

Code Optimization
➢ Optimization is a program transformation technique, which tries to improve the code by making it
consume less resources (i.e. CPU, Memory) and deliver high speed.
➢ Code optimization is any method of code modification to improve code quality and efficiency.
➢ The process of code optimization involves-
o Eliminating the unwanted code lines
o Rearranging the statements of the code
➢ Goals of code optimization:
o Remove redundant code without changing the meaning of program.
o Reduce code size
o Efficient memory usage
o Yielding better performance.
➢ The i/p and o/p of the code optimizer is intermediate code. However, the code is an optimized set
of intermediate stmts.
Some optimization techniques include
I. Dead code elimination
Variable is live at a point in a program if its value can be used subsequently otherwise, it is dead at that
point. A related idea is dead (or useless) code statements that compute values that never get used.
Thus, dead code plays no role in any program operation and therefore it can simply be eliminated. Code
that is unreachable or that does not affect the program (e.g. dead stores) can be eliminated too.
Example:1
In the example below, the value assigned to i is never used, and the dead store can be eliminated. The
first assignment to global is dead, and the third assignment to global is unreachable; both can be
eliminated.
int global;
void f ()
{
int I;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable */
}
Code fragment after dead code elimination.

Compiled by: Dawit K. 1


Compiler Design
int global;
void f ()
{
global = 2;
return;
}
Example:2
Before After optimization
a := x * x a := x * x
b := 3 f := a + a
c := x g := 6 * f
d := a
e := 6
f := a + a
g := 6 * f

II. Loop optimizations


➢ Loop Optimization is the process of increasing execution speed and reducing the overheads
associated with loops. It plays an important role in improving cache performance and making
effective use of parallel processing capabilities.
➢ Loop Optimization is a machine independent optimization.
➢ Loops can be optimized by the following techniques
o Frequency Reduction (Code Motion)
Code motion is used to decrease the amount of code in loop. This transformation takes a
statement or expression which can be moved outside the loop body without affecting the
semantics of the program, is moved outside the loop. If the result of a statement or expression
does not change within a loop, and it has no external side-effect

Example: Initial code: optimized code


While(i<100) t= sin(x)/cos(x);
{ While(i<100) {
a=sin(x)/cos(x) + i; a=t+i;
i++; i++;
} }
o Loop Unrolling
Execute loop body multiple times at each iteration Loop unrolling is a loop transformation
technique that helps to optimize the execution time of a program. We basically remove or reduce
iterations. Loop unrolling increases the program’s speed by eliminating loop control
instruction and loop test instructions.
Example
Initial code: optimized code
for (int i=0; i<5; i++) Printf (“hello \n”);
Printf (“hello \n”); Printf (“hello \n”);
Printf (“hello \n”);
Printf (“hello \n”);
Printf (“hello \n”);

Compiled by: Dawit K. 2


Compiler Design

o Loop Jamming
Loop jamming is the combining the two or more loops in a single loop. It reduces the time taken
to compile the many number of loops.
Example:
Initial code optimized code
for (int i=0; i<5; i++) for (int i=0; i<5; i++)
a= i+5; { a= i+5;
for (int i=0; i<5; i++) b= i+ 10;
b= i+ 10; }
III. Induction variables
➢ Are variables such that every time they change value, they are incremented or decremented.
➢ Basic induction variable: induction variable whose only assignments within a loop are of the
form: i = i +/- C, where C is a constant.
➢ Primary induction variable: basic induction variable that controls the loop execution
(for i=0; i<100; i++) i (register holding i) is the primary induction variable.
➢ Derived induction variable: variable that is a linear function of a basic induction variable.
Example:
Initial code optimized code
s=3*i+1; s=3*i+1;
while(i<10) { while(s<31)
a[s]=a[s]-2; { a[s]=a[s]-2;
i=i+2//induction variable s=s+6;
s=s+6; }
}

IV. Reduction in strength


➢ Strength reduction is an optimization technique which consists of the replacement of one type of
operation by another operation that takes less time to execute.
➢ Reduction in strength replaces expensive operations by equivalent cheaper ones on the target
machine. Certain machine instructions are considerably cheaper than others and can often be used as
special cases of more expensive operators.
➢ Example 1, x² is invariably cheaper to implement as x*x than as a call to an exponentiation routine.
Fixed-point multiplication or division by a power of two is cheaper to implement as a shift.

Example 2
Initial code optimized code
2
B= A B=A+A
The expression “A x 2” is replaced with the expression “A + A”.
For above example, on most computers a multiplication takes more time to execute than does an addition.
This is especially true on many of today’s minicomputers

Compiled by: Dawit K. 3

You might also like