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

Code Optimization

Code optimization in compiler design enhances the performance and efficiency of executable code through various techniques that do not alter program functionality. It is classified into machine independent and machine dependent optimizations, with peephole optimization focusing on small code segments for performance improvement. Loop optimization further increases execution speed and reduces overhead associated with loops, employing techniques such as code motion, induction variable elimination, and loop unrolling.

Uploaded by

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

Code Optimization

Code optimization in compiler design enhances the performance and efficiency of executable code through various techniques that do not alter program functionality. It is classified into machine independent and machine dependent optimizations, with peephole optimization focusing on small code segments for performance improvement. Loop optimization further increases execution speed and reduces overhead associated with loops, employing techniques such as code motion, induction variable elimination, and loop unrolling.

Uploaded by

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

Code Optimization in Compiler Design

Code optimization is a crucial phase in compiler design aimed at enhancing the


performance and efficiency of the executable code. By improving the quality of the
generated machine code optimizations can reduce execution time, minimize resource
usage, and improve overall system performance. This process involves the various
techniques and strategies applied during compilation to produce more efficient code
without altering the program’s functionality.
The code optimization in the synthesis phase is a program transformation technique,
which tries to improve the intermediate code by making it consume fewer resources (i.e.
CPU, Memory) so that faster-running machine code will result. The compiler optimizing
process should meet the following objectives:
 The optimization must be correct, it must not, in any way, change the meaning of the
program.
 Optimization should increase the speed and performance of the program.
 The compilation time must be kept reasonable.
 The optimization process should not delay the overall compiling process.

Types of Code Optimization


The optimization process can be broadly classified into two types:
 Machine Independent Optimization: This code optimization phase attempts to
improve the intermediate code to get a better target code as the output. The part of
the intermediate code which is transformed here does not involve any CPU
registers or absolute memory locations.
 Machine Dependent Optimization: Machine-dependent optimization is done after
the target code has been generated and when the code is transformed according
to the target machine architecture. It involves CPU registers and may have
absolute memory references rather than relative references. Machine-dependent
optimizers put efforts to take maximum advantage of the memory hierarchy.

Peephole Optimization in Compiler Design



Peephole optimization is a type of code Optimization performed on a small part of


the code. It is performed on a very small set of instructions in a segment of code.
The small set of instructions or small part of code on which peephole optimization is
performed is known as peephole or window.
It basically works on the theory of replacement in which a part of code is replaced by
shorter and faster code without a change in output. The peephole is machine-
dependent optimization.
Objectives of Peephole Optimization:
The objective of peephole optimization is as follows:
1. To improve performance
2. To reduce memory footprint
3. To reduce code size
Peephole optimization improves the efficiency of compiled code by eliminating
unnecessary instructions in small code sequences. It’s a key technique in modern
compiler design.
Peephole Optimization Techniques
A. Redundant load and store elimination: In this technique, redundancy is
eliminated.
Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;

Optimized code:
y = x + 5;
w = y * 3; //* there is no i now

//* We've removed two redundant variables i & z whose value were just being copied
from one another.
B. Constant folding: The code that can be simplified by the user itself, is simplified.
Here simplification to be done at runtime are replaced with simplified code to avoid
additional computation.
Initial code:
x = 2 * 3;

Optimized code:
x = 6;
C. Strength Reduction: The operators that consume higher execution time are
replaced by the operators consuming less execution time.
Initial code:
y = x * 2;

Optimized code:
y = x + x; or y = x << 1;

Initial code:
y = x / 2;
Optimized code:
y = x >> 1;
D. Null sequences/ Simplify Algebraic Expressions : Useless operations are
deleted.
a := a + 0;
a := a * 1;
a := a/1;
a := a - 0;
E. Combine operations: Several operations are replaced by a single equivalent
operation.
F. Deadcode Elimination:- Dead code refers to portions of the program that are
never executed or do not affect the program’s observable behavior. Eliminating dead
code helps improve the efficiency and performance of the compiled program by
reducing unnecessary computations and memory usage.
Initial Code:-
int Dead(void)
{
int a=10;
int z=50;
int c;
c=z*5;
printf(c);
a=20;
a=a*10; //No need of These Two Lines
return 0;
}
Optimized Code:-
int Dead(void)
{
int a=10;
int z=50;
int c;
c=z*5;
printf(c);
return 0;
}

Loop Optimization in Compiler Design


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. Most
execution time of a scientific program is spent on loops.
Loop Optimization is a machine independent optimization. Whereas Peephole
optimization is a machine dependent optimization technique.
Decreasing the number of instructions in an inner loop improves the running time of a
program even if the amount of code outside that loop is increased.
Loop Optimization Techniques
In the compiler, we have various loop optimization techniques, which are as follows:

1. Code Motion (Frequency Reduction)

In frequency reduction, the amount of code in the loop is decreased. A statement or


expression, which can be moved outside the loop body without affecting the semantics
of the program, is moved outside the loop.
Example:
Before optimization:
while(i<100)
{
a = Sin(x)/Cos(x) + i;
i++;
}

After optimization:

t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}

2. Induction Variable Elimination

If the value of any variable in any loop gets changed every time, then such a variable is
known as an induction variable. With each iteration, its value either gets incremented or
decremented by some constant value.
Example:
Before optimization:
B1
i:= i+1
x:= 3*i
y:= a[x]
if y< 15, goto B2
In the above example, i and x are locked, if i is incremented by 1 then x is incremented
by 3. So, i and x are induction variables.
After optimization:
B1
i:= i+1
x:= x+4
y:= a[x]
if y< 15, goto B2

3. Strength Reduction

Strength reduction deals with replacing expensive operations with cheaper ones like
multiplication is costlier than addition, so multiplication can be replaced by addition in
the loop.
Example:
Before optimization:
while (x<10)
{
y := 3 * x+1;
a[y] := a[y]-2;
x := x+2;
}
After optimization:
t= 3 * x+1;
while (x<10)
{
y=t;
a[y]= a[y]-2;
x=x+2;
t=t+6;
}

4. Loop Invariant Method

In the loop invariant method, the expression with computation is avoided inside the loop.
That computation is performed outside the loop as computing the same expression
each time was overhead to the system, and this reduces computation overhead and
hence optimizes the code.
Example:
Before optimization:
for (int i=0; i<10;i++)
t= i+(x/y);
...
end;

After optimization:
s = x/y;
for (int i=0; i<10;i++)
t= i+ s;
...
end;

5. Loop Unrolling

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:
Before optimization:

for (int i=0; i<5; i++)


printf("Pankaj\n");

After optimization:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");

6. Loop Jamming

Loop jamming is combining two or more loops in a single loop. It reduces the time taken
to compile the many loops.
Example:
Before optimization:

for(int i=0; i<5; i++)


a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;

After optimization:

for(int i=0; i<5; i++)


{
a = i + 5;
b = i + 10;
}

You might also like