Redirected
1,533 views

2 Answers

Best answer
11 votes
11 votes

In compiler design, peephole optimization is a kind of optimization performed over a very small set of instructions in a segment of generated code. The set is called a "peephole" or a "window". It works by recognising sets of instructions that can be replaced by shorter or faster sets of instructions.

For example lets consider this example where compiler removing redundant code,

a = b + c;
d = a + e;

is straightforwardly implemented as

MOV b, R0  # Copy b to the register
ADD c, R0  # Add  c to the register, the register is now b+c
MOV R0, a  # Copy the register to a
MOV a, R0  # Copy a to the register
ADD e, R0  # Add  e to the register, the register is now a+e [(b+c)+e]
MOV R0, d  # Copy the register to d

but can be optimised to

MOV b, R0 # Copy b to the register
ADD c, R0 # Add c to the register, which is now b+c (a)
MOV R0, a # Copy the register to a
ADD e, R0 # Add e to the register, which is now b+c+e [(a)+e]
MOV R0, d # Copy the register to d

Here is some more great examples are given. Have a look. :) 

And you asked what is peephole in peephole optimization? 

Answer for this is : A short sequence of target instructions that may be replaced by a shorter/faster sequence.

selected by
2 votes
2 votes

Peephole Optimization

This optimization technique works locally on the source code to transform it into an optimized code. By locally, we mean a small portion of the code block at hand. These methods can be applied on intermediate codes as well as on target codes. A bunch of statements is analyzed and are checked for the following possible optimization:

Redundant instruction elimination

At source code level, the following can be done by the user:

int add_ten(int x)
   {
   int y, z;
   y = 10;
   z = x + y;
   return z;
   }
int add_ten(int x)
   {
   int y;
   y = 10;
   y = x + y;
   return y;
   }
int add_ten(int x)
   {
   int y = 10;
   return x + y;
   }
   
   
int add_ten(int x)
   {
   return x + 10;
   }
   
   
   

At compilation level, the compiler searches for instructions redundant in nature. Multiple loading and storing of instructions may carry the same meaning even if some of them are removed. For example:

  • MOV x, R0
  • MOV R0, R1

We can delete the first instruction and re-write the sentence as:

MOV x, R1

This description precisely explains in a lucid way. 

Refer the link below for more information

https://www.tutorialspoint.com/compiler_design/compiler_design_code_generation.htm

Hope it helps u. 

Related questions

1 votes
1 votes
1 answer
1
ManojK asked May 6, 2016
1,260 views
What is dynamic variable.? Is there any benefit of using dynamic variable instead of static variable?
6 votes
6 votes
0 answers
2
PravardhanGate asked Feb 12, 2018
6,606 views
Any one who attended the interview for ISRO SCIENTIST, please share your experience....
1 votes
1 votes
7 answers
3
sh!va asked Dec 6, 2016
1,436 views
Conceptual question:What is the difference between program and process?any simple ,clear answer plz..