# Code Optimization Technique

Again Freestudy9 comes with new post related to Code Optimization Technique. Additionally, we had tried to make it easy as possible as well.

## 1.** Common subexpressions elimination**

- Compile-time evaluation means shifting of computations from runtime to compile time.computations
- There are two methods used to obtain the compile time evaluation.
**Folding**- In the folding technique, the computation of constant done at compile time instead of runtime.

Example : length = (22/7)*d - Here folding is implied by performing the computation of 22/7 at compile time.
**Constant propagation**- In this technique, the value of the variable is replaced and computation of an expression is expression done at compilation time.

Example:

pi = 3.14; r = 5;

Area = pi * r * rr; - Here at the compilation time, the value of pi is replaced by 3.14 and r by 5 then the computation of 3.14 * 5 * 5 is done during compilation.

**2. Common subexpressions elimination: Code Optimization Technique**

- The common subexpression is an expression appearing repeatedly in the program which computed previously.
- If the operands of this subexpression do not get changed at all then result of such subexpression used instead of rere-computing it each time.

Example:

t1 := 4 * it1=4*i

t2 := a[t1]t2=a[t1]

t3 := 4 * jt3=4*j

t4 : = 4 * it5=n

t5:= nt6=b[t1]+t5

t6 := b[t4]+t5 - The common sub expression t4:=t4:=4*i is eliminated as its computation is already in t1 and value of i is not been changed from definition to use.

**3. Variable propagation: Code Optimization Technique**

- Variable propagation means a use of one variable instead of another.

Example:

x = pi;

area = x * r * r; - The optimization using variable propagation can do as follows, area = pi * r * r;
- Here the variable x eliminated. Here the necessary condition that a variable must be assigned to another variable or some constant.

**4. Code movement: Code Optimization Technique**

There are two basic goals of code movement:

I. To reduce the size of the code.

II.To reduce the frequency of execution of code.

Example:

for(i=0;i<=10;i++)temp=y*5

{for(i=0;i<=10;i++)

x=y*5;{

k=(y*5)+50;x=temp;

}k=(temp) + 50;

}

**Loop invariant computation**

- Loop invariant optimization can obtain by moving some amount of code outside the loop and placing it just before entering the loop.
- This method also called code motion.

Example:

While(i<=max-1)N=max-1;

{While(i<=N)

sum=sum+a[i];{

}sum=sum+a[i];

}

**5. Strength reduction: Code Optimization Technique**

- The strength of certain operators is higher than others.
- For instance strength of * is higher than +.
- In this technique, the higher strength operators can replace by lower strength operators.
- Example:

for(i=1;i<=50;i++)

{

count = i*7;

} - Here we get the count values as 7, 14, 21 and so on up to less than 50.
- This code can replaced by using strength reduction as follows

temp=7;

for(i=1;i<=50;i++)

{

count = temp;

temp = temp+7;

}

**6. Dead code elimination (**Code Optimization Technique**)**

- A variable said to be live in a program if the value contained into is subsequently.
- On the other hand, the variable said to be dead at a point in a program if the value contained in it is never been used. The code containing such a variable supposed to be a dead code. And an optimization can perform by eliminating such a dead code.

Example:

i=0;

if(i==1)

{

a=x+5;

} - If statement dead code as this condition will never get satisfied hence, statement can eliminat and ooptimization can be done.

**Related Search**

Compiler Design, Transformations on Basic Block, Peephole Optimization, Free Study.

## Leave a Reply