Sie sind auf Seite 1von 5

Watumull Institute of Electronics Engineering & Computer Technology Worli, Mumbai 400 018.

Experiment No: 5 Aim: Design and Implementation of Code Optimization techniques. Software Used: c / java language Theory: Code Optimization: An optimizing compiler is perhaps one of the most complicated, most powerful, and most interesting programs in existence. Optimization within a compiler is concerned with improving in some way the generated object code while ensuring the result is identical. Technically, a better name for this chapter might be "Improvement", since compilers only attempt to improve the operations the programmer has requested. Optimizations fall into three categories:

Speed; improving the runtime performance of the generated object code. This is the most common optimization Space; reducing the size of the generated object code Safety; reducing the possibility of data structures becoming corrupted (for example, ensuring that an illegal array element is not written to)

Unfortunately, many "speed" optimizations make the code larger, and many "space" optimizations make the code slower -- this is known as the space-time tradeoff.
Common Optimization Algorithms

Common optimization algorithms deal with specific cases: 1. 2. 3. 4. 5. 6. 7. Dead Code Elimination Common Sub-expression Elimination Constant Propagation Variable Propagation Code Motion Induction Variable Elimination Strength Reduction

1. Dead code Elimination:A variable said to be live in a program if the value contained into it is used sequentially. The variable is said to be dead at a point in a program if the value contained into it is never used. The code containing such a variable supposed to be a dead code. And an optimization can be performed by eliminating such a dead code.

Prepared By: Avinash Gondal

Watumull Institute of Electronics Engineering & Computer Technology Worli, Mumbai 400 018.
For example: i=j; x=i+10; The optimization can be performed by eliminating the assignment statement i=j. This assignment statement is called dead assignment. 2. Common Sub-expression Elimination:This is an expression appearing repeatedly in program which is computed previously. If the operands of such sub-expression dont get changed at all then result of sub expression is used instead of re-computing it each time. For example : t1:=4*i; t2:=a [t1]; t3:=4*j; t4:=4*i; t5:=n t6:=b [t4] +t5 3. Code Motion Its a technique which moves the code outside the loop. Here is the name. If there lies some expression in the loop whose result remains unchanged even after executing the loop for several time, then such an expression should be placed just before the loop (i.e. outside the loop). Here before the loop means at the entry of the loop. t1:=4*i t2:=a [t1] t3:=4*j t5:=n t6:=b [t1] +t5

Prepared By: Avinash Gondal

Watumull Institute of Electronics Engineering & Computer Technology Worli, Mumbai 400 018.
For example: while (i<=max-1) { sum: =sum + a[i]; } The above code can be optimized by removing the computation of max-1 outside the loop. Hence the optimized code can ben=max-1; while (i<=n) { sum: =sum +a[i]; } 4. Induction Variable Elimination A variable x is called as induction variable of loop L if the value of variable gets changed every time. It is either decremented / incremented by some constant. For example Consider the block given below B1 i=t+1; t1=4*j; t2:=a [t1]; if t2<10 goto B1.

Prepared By: Avinash Gondal

Watumull Institute of Electronics Engineering & Computer Technology Worli, Mumbai 400 018.
In the above code the values of i & t1 are in locked state. That is, when value of i gets incremented by 1 then t1 get incremented by 4. Hence i & t1 are induction variables. If there are two or more induction variables in a loop, it may be possible to get rid of all but one. 5. Strength Reduction The strength of certain operators is higher than others. For example : strength of * is higher than +. In strength reduction technique the higher strength operators can be replaced by lower strength operators. For example: for(i=10;i<=50;i++) { count :=1*7 }

Here we get the value of count as 7,14,21& so on upto 50. This code can be replaced by using strength reduction as follows. temp:=7 for(i=1;i<=50;i++) { count:=temp; temp:=temp+7; }

Prepared By: Avinash Gondal

Watumull Institute of Electronics Engineering & Computer Technology Worli, Mumbai 400 018.
Algorithm: Write as per your program

Conclusion: Hence we studied code optimization techniques.

Prepared By: Avinash Gondal