Ticker

6/recent/ticker-posts

Peephole Optimization in compiler AND Optimize the Intermediate code using small window.

 Peephole optimization is an optimization technique that is used to improve the performance of  target code.In which changing the small set of instructions to an equivalent set that has better performance.Repeated passes of improvement are applied on code to get maximum benefits.Peephole means small window is moved over target code to apply some transformation.It improve the code locally.Short sequence of target code instruction are examined and are replace by faster sequence.

Requirement of Peephole Optimization

  • Program will run faster on performing optimization.
  • Increase program efficiency after code optimization.
  • Produce better object code.
Technique of peephole optimization
  • Redundant Load and Store Instruction Elimination.
  • Simplify Algebraic Expression.
  • Strength Reduction.
  • Replace Slower instruction with faster.
  • Dead code elimination.
  • Multiple Jump Elimination.
Redundant Load and Store Instruction Elimination
To Remove Redundant Load and Store Instruction optimize the target code.
For example
p=a+b
q=p+c

Mov  a, R0
ADD  b, R1
Mov   R0, p
Mov  p,R0

Here Instruction Mov p,R0 can eliminated because p is already in R0.

Simplify Algebric Expression
Statement just like
p=p+0
p=p*1
p=p/1
p=p-0

can be eliminated in peephole optimization because adding identity(0) in p or multiply with 1 will not make any difference in the value of p.Remove that statement in optimization is called simplify Algebric expression.

Reduction in Strength
Some Instruction are cheaper than other .To improve performance of the intermediate code replace these instruction by cheaper instruction.

Example

Addition and Subtraction is cheaper than multiplication and division.
x*2 
That instruction is replace by Left shift instruction.

x/2
That instruction is replace by Right shift instruction.
X2

That instruction is replace by x*x.

Replace Slower instruction with faster
Some instruction are slower than other .To improve performance of the intermediate code replace these instruction by faster instruction.

Example
Add, #1,R
That instruction is replace with
Inc R

Dead code Elimination
Dead code is useless code which can be removed from the program.
Example
int main()
{
int a=10;
int b=20;
int c;
c=a+b;
return c;
b=5;
b=b+2;
return 0;
}


In this program 
b=5;
b=b+2;

is dead code .

Multiple Jump Elimination
There can be jumps to jumps in intermediate code either conditional jump or unconditional jump which can be removed.

For Example
goto L1
-
-
-
L1: goto L2
-
-
-
L2
That is unconditional jump.

Example
if p<q goto L1

L1: goto L2

L2:

That is conditional  jump.













Post a Comment

0 Comments