in Compiler Design
1,294 views
3 votes
3 votes
What is peephole (In peephole Optimization technique) ?
in Compiler Design
by
1.3k views

2 Comments

is this present in GATE 2020 syllabus?
0
0
yes
0
0

2 Answers

11 votes
11 votes
Best answer

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
by

1 comment

good .Let,s see other answer too.........yes

0
0
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