Group Members
Rabbia Nazeer
Waqas Nazeer
Topic
What is code Optimization?
• Is a set of methods of code modification to
improve code quality and efficiency.
• Transforming a program into an equivalent but
better form.
• Elimination of extra code.
• It can be performed by automatic optimizers.
• An optimizer is either a specialized software tool
Why is code optimized?
• Compiler can be considered to have efficient performance if it translates a program of
about 1000 lines in just few seconds.
• Code optimization is done to get satisfactory performance of compiler.
Goals of Code Optimization
• To make the code generation faster.
• To Make the code smaller.
• To make the compiler more efficient.
Disadvantages of code Optimization
• Rearrange and removal of code will reduce the effectiveness of debuggers.
• Compiler that you are using may contain bugs that only exist when it is performing
Disadvantages of Code Optimization
(cont.)
• Optimization of high level language is
Optimization is done of 3 level of
program:
1) Source level Optimization.
2) Intermediate Code Optimization.
Source level Optimization:
• It does not requires any knowledge of machine
instructions.
• This level is machine independent.
• It can be done by replacing a slow or inefficient
algorithm with a quick algorithm.
• For example Bubble Sort is not much efficient and
Intermediate Code Optimization:
• Optimization done on intermediate code.
• Most Optimizations are performed on intermediate level.
Intermediate Code Optimization
• Techniques used in intermediate code optimization are:
1) Local Optimization.
2) Loop Optimization.
Local Optimization
• Optimization done with in a single basic block.
• Techniques:
1) Constant Folding.
2) Copy Propagation.
3) Algebraic Simplification.
4) Elimination of common sub expressions.
5) Reduction in operator strength.
Constant Folding:
• To computer constant expressions at compile
time.
• For example:
int a=4; int a=4 i=2+a; i=6;
j=i*5+b; j=30+b;
• String literals can be constant folded, such as
Copy Propagation
• It identifies and eliminates those statements which are redundant.
• Example:
• int i=0; int i=0
• A=b+c; a=b+c
Algebraic Simplification
• Simplifications use algebraic properties.
• Useless instructions can be removed entirely via algebraic identities.
a*1 a ( Multiplicative Identity) a/1 a
a*0 0
a+0 a (Additive identity) a-0 a
Elimination of common Sub expression
• Replace re computation of expression by use of temp which holds value.
Examples :
1) a = b * c + g; tmp = b * c; d = b * c * e; a = tmp + g; d = tmp * e;
• 2) j= D+C*e temp=D+C k=D+C/F j=temp*e
Common Sub expression
Elimination cont.
3) A[ i+1 ] = B[ i+1 ]; tmp = i+1;
A[ tmp ] = B[ tmp ];
• Detection and elimination of redundant computations is the major goal of an
Reduction in operator strength
• Replacing an expensive operator by cheaper one.
• For example:
Multiplying integers usually take longer than adding them.
x = 2 * y; x = y + y;
Inline expansion of code.
• Replaces a function with its body of the code.
• It might occupy more memory but reduced compile time.
Example:
Loop Optimization
Loop optimization is the process of the
increasing execution speed and reducing the overheads associated of loops.
Techniques used in loop optimization:
1) Loop in-variant code hoisting.
2) Loop Fusion.
Loop in variant code hoisting
• Loop-invariant code consists of statements or
expressions which can be moved outside the body of a loop.
• It do not affect the semantics of the program.
• Example:
for( I =0 ; i< 10 ; i++) {
a[i] = b[i] + c/d; }
Int temp= c/d;
for( I =0 ; i< 10 ; i++) {
Loop Fusion
• Replacing multiple loops having identical headers with a single one.
• It is done to reduce number of tests. Example:
int i, a[100], b[100];
for (i = 0; i < 100; i++) a[i] = 1;
for (i = 0; i < 100; i++) b[i] = 2;
int i,
a[100], b[100];
for (i = 0; i < 100; i++) {
Loop Collapsing
• Flatten a multi dimensional loop.
• Nested loops can be collapsed into a
single-nested loop to reduce loop overhead and improve run-time performance.
Example:
for (i=0; i<N; i++) for (j=0; j<M; j++) ... a[ i ][ j ] ... ;
for (ij=0; i<M*N; ij++) {
Global Optimization
• Whole program is analyzed in global optimization.
• It needs data flow analysis.
It includes:
a) Remove unreferenced variables.
b) Avoid calculation whose results are not used.
c) Remove the code which is not called.
Remove unreferenced variables
Int main() int main() { {
int j=0; Int []temp=new int[5]; int [] temp=new int[5];
for(int i=0;i<tem.Length;i++) for(int i=0;i<temp.Length;i++) { {
Console.WriteLine(temp[i]); Console.WriteLine(temp[i]); } }
} }
Avoid calculation whose results are not
used
Int main() { int a; int b; int c;String [] text=textBox1.Text; c=a+b;
for(int i=0;i<text;Length;i++) {
if(temp[i]==‘a’) {
Console.WriteLine(“string accepted”); }
else
Console.writeLine(“String rejected”); }
}
Remove the code which is not called.
int SQR(int a); void main(void) { int x=2; Int square; square=x*x; }
int SQR(int a) {
return a*a; }
We have declared the SQR() function and also defined it but the function is not
called any where in program This function is taking extra memory
Remove un initialized variables
• Void main()
• {
• int i;
• int temp=0;
• int d=5;
• int f=2;
• temp=d+f;
return 0; }
Dataflow Analysis
• Technique for gathering information about
the possible set of values calculated at various points in a computer program.
• Control flow graph is used to determine those parts of a program to which a particular value assigned to a variable.
Target Level Optimizations
• Also called machine dependent optimization.
Target Level Optimizations
Techniques for Machine Dependent Optimization.
1) Register Allocation.
a) Graph Coloring.
2) Instruction Scheduling.
Register Allocation
• It refers to choosing which values are kept in
which registers during program execution.
• Registers are fastest kind of memory on the
target machine.
• Storing and accessing variables from registers is
Register Allocation
• Efficient use of registers is very important, it causes to reduce memory access.
• Virtual registers are mapped on to physical registers
Example
Register Allocation by Graph Coloring
• It is used when register is needed for
computation but all available registers are in use.
• Then the content of one of the used register
must be stored into a memory location in order to free up the register.
Graph coloring technique
• Variable c wants
• to be loaded in a
register but there is no free register so a register whose value is currently not in use will shift its variable in
Instruction scheduling
• It involves reordering the instructions before machine code is generated.
Peephole optimization
Very simple and effective technique for improving target code.
Uses a window of only few consecutive instructions at a time(called peephole)
It includes:
a) Redundant instruction elimination.
c) Elimination of load of value when previous instruction stored
that value from a register to a memory location.
Peephole Optimization
• Lets see an example f=a+10 Store R3,f store R4,f load R1,a Load R2,10
Add R3,R1,R2 load R1,a Load R2,10 Add R3,R1,R2
Store R4,f Load R1,a Add R3,R1,10