0% found this document useful (0 votes)
27 views12 pages

Unit 5

The document discusses code optimization techniques including peephole optimization and loop optimization. Peephole optimization aims to improve performance, reduce memory footprint, and reduce code size. Techniques include redundant load/store elimination, strength reduction, and replacing slower instructions with faster ones. Loop optimization focuses on increasing execution speed and reducing overheads associated with loops through techniques like frequency reduction, loop unrolling, and loop jamming. Data flow analysis is also discussed as a way for compilers to perform optimizations by determining the flow of data and definitions/uses of variables.

Uploaded by

savatid730
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)
27 views12 pages

Unit 5

The document discusses code optimization techniques including peephole optimization and loop optimization. Peephole optimization aims to improve performance, reduce memory footprint, and reduce code size. Techniques include redundant load/store elimination, strength reduction, and replacing slower instructions with faster ones. Loop optimization focuses on increasing execution speed and reducing overheads associated with loops through techniques like frequency reduction, loop unrolling, and loop jamming. Data flow analysis is also discussed as a way for compilers to perform optimizations by determining the flow of data and definitions/uses of variables.

Uploaded by

savatid730
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/ 12

UNIT-5

Code Optimization

Code Optimization is an approach to enhance the performance of the code.


The process of code optimization involves-
 Eliminating the unwanted code lines
 Rearranging the statements of the code

Advantages-

The optimized code has the following advantages-


 Optimized code has faster execution speed.
 Optimized code utilizes the memory efficiently.
 Optimized code gives better performance.

Code Optimization Techniques-

Important code optimization techniques are-


Machine dependent: Peephole optimization

Machine independent: Loop optimization


Peephole optimization

Peephole optimization is an optimization method performed on a small set of


compiler generated instructions; the small set is known as peephole optimization in
compiler design or window. There are three objectives of peephole optimization
Improve performance, Reduce memory footprint, and Reduce code size.
The objectives of peephole optimization are as follows:
1. Improve performance
2. Reduce memory footprint
3. Reduce code size.

There are various peephole optimization techniques.


1.Redundant Load and Store
In this optimization, the redundant operations are removed. For example, loading
and storing values on registers can be optimized.
For example,
a= b+c
d= a+e
It is implemented on the register(R0) as
MOV b, R0;
ADD c, R0;
MOV R0, a;
MOV a, R0;
ADD e, R0 ;
MOV R0, d;
After removing
MOV b, R0;
ADD c, R0;
MOV R0, a;
ADD e, R0;
MOV R0, d;

Strength Reduction
In strength reduction optimization, operators that consume higher execution time
are replaced by the operators consuming less execution time.

b=a ²
Can be replaced by
B=a*a;
Initial code:
n = a * 2;
Optimized code:
b= a << 1;
//left shifting the bit
Initial code:
b = a / 2;
Optimized code:
b = a >> 1;
// right shifting the bit by one will give the same result

Simply Algebraic Expressions


The algebraic expressions that are useless or written inefficiently are transformed.
For example:
a=a+0
a=a*1
a=a/1
a=a-0
//All these above expression are causing calculation overhead.
// These can be removed for optimization

Replace Slower Instructions With Faster


Slower instructions can be replaced with faster ones, and registers play an
important role. For example, a register supporting unit increment operation will
perform better than adding one to the register. The same can be done with many
other operations, like multiplication.
Add #1
SUB #1
//The above instruction can be replaced with
// INC R
// DEC R
//If the register supports increment and decrement

Dead code Elimination


The dead code can be eliminated to improve the system's performance; resources
will be free and less memory will be needed.
int dead(void)
{
int a=1;
int b=5;
int c=a+b;
return c;
cout<<”hello”;
// c will be returned
// The remaining part of code is dead code, never reachable

common subexpression elimination

a = 10;
b = a + 1 * 2;
c = a + 1 * 2;
//’c’ has common expression as ‘b’
d = c + a;
After elimination –
a = 10;
b = a + 1 * 2;
d = b + a;
Loop Optimization
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 Techniques:

1. Frequency Reduction (Code Motion):


In frequency reduction, the amount of code in 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:
Initial code:
a=10;
y=1;
z=2;
while(a>0)
{
X=y+z;
If(a%x==0)
{printf(“%d”,a);

}
}

Optimized code:
a=10;
y=1;
z=2;
X=y+z;
while(a>0)
{
If(a%x==0)
{printf(“%d”,a);

}
}

2. 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:
Initial code:

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


printf("Neha\n");

Optimized code:

printf("Neha\n");
printf("Neha\n");
printf("Neha\n");
printf("Neha\n");
printf("Neha\n");

3. 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:

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


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

Optimized code:
for(int i=0; i<5; i++)
{
a = i + 5;
b = i + 10;
}
Data flow analysis in Compiler
It is the analysis of flow of data in control flow graph, i.e., the analysis
that determines the information regarding the definition and use of data
in program. With the help of this analysis, optimization can be done. In
general, its process in which values are computed using data flow
analysis. The data flow property represents information that can be
used for optimization.
Basic Terminologies –

 Definition Point: a point in a program containing some definition.


 Reference Point: a point in a program containing a reference to a
data item.
 Evaluation Point: a point in a program containing evaluation of
expression.

Data Flow Properties –


 Available Expression – A expression is said to be available at a
program point x if along paths its reaching to x. A Expression is
available at its evaluation point.
An expression a+b is said to be available if none of the operands
gets modified before their use.
Example –

 Advantage –
It is used to eliminate common sub expressions.

 Reaching Definition – A definition D is reaches a point x if there is


path from D to x in which D is not killed, i.e., not redefined.
Example –
 Advantage –
It is used in constant and variable propagation.

 Live variable – A variable is said to be live at some point p if from p


to end the variable is used before it is redefined else it becomes
dead.
Example –
 Advantage –
1. It is useful for register allocation.
2. It is used in dead code elimination.
 Busy Expression – An expression is busy along a path if its
evaluation exists along that path and none of its operand definition
exists before its evaluation along the path.
Advantage –
It is used for performing code movement optimization.

Global data flow analysis


o To efficiently optimize the code compiler collects all the information
about the program and distribute this information to each block of the
flow graph. This process is known as data-flow graph analysis.
o Certain optimization can only be achieved by examining the entire
program. It can't be achieve by examining just a portion of the program.
o For this kind of optimization user defined chaining is one particular
problem.
o Here using the value of the variable, we try to find out that which
definition of a variable is applicable in a statement.

Based on the local information a compiler can perform some optimizations.


For example, consider the following code:

1. x = a + b;
2. x=6*3
o In this code, the first assignment of x is useless. The value computer for
x is never used in the program.
o At compile time the expression 6*3 will be computed, simplifying the
second assignment statement to x = 18;

Some optimization needs more global information. For example, consider the
following code:

1. a = 1;
2. b = 2;
3. c = 3;
4. if (....) x = a + 5;
5. else x = b + 4;
6. c = x + 1;

In this code, at line 3 the initial assignment is useless and x +1 expression can
be simplified as 7.

But it is less obvious that how a compiler can discover these facts by looking
only at one or two consecutive statements. A more global analysis is required
so that the compiler knows the following things at each point in the program:
Video

o Which variables are guaranteed to have constant values


o Which variables will be used before being redefined

Data flow analysis is used to discover this kind of property. The data flow
analysis can be performed on the program's control flow graph (CFG).
The control flow graph of a program is used to determine those parts of a
program to which a particular value assigned to a variable might propagate.

You might also like