[go: up one dir, main page]

0% found this document useful (0 votes)
19 views12 pages

Peephole Optimization

Uploaded by

nexexa6242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Peephole Optimization

Uploaded by

nexexa6242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Computer Science & Engineering

UIT-RGPV

Topic: Peephole Optimization

Course: Compiler Design (CS701)

By:
Mr. Praveen Yadav
Assistant Professor
DoCSE, UIT-RGPV
Bhopal
Peephole Optimization
• A simple but effective technique for locally improving the target
code is peephole optimization.
• Peephole optimization is done by examining a sliding window of
target instructions (called the peephole) and replacing
instruction sequences within the peephole by a shorter or faster
sequence, whenever possible.
• Peephole optimization can also be applied directly after
intermediate code generation to improve the intermediate
representation.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2


Characteristic of Peephole Optimizations
Peephole optimizations Technique include following
Transformations to improve target code:
1. Redundant-instruction elimination
2. Unreachable code elimination
3. Flow-of-control optimizations
4. Algebraic Simplifications
5. Reduction-in-strength transformations
6. Use of machine idioms

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 3


1. Redundant-instruction Elimination
Consider the following instructions:
1. MOV R0 , X
2. MOV X, R0
• The second instruction can be deleted since the first instruction
ensures that the value of X is already loaded into register R0.
• However, it cannot be deleted in a situation when, it has a label
which makes it difficult to identify that whether the first
instruction is always executed before the second.
• To ensure that this kind of transformation in the target code
would be safe, the two instructions must be in the same basic
block.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 4


2. Unreachable code elimination
• Another opportunity for peephole optimization is the removal of
unreachable instructions.
• Removing an unlabeled instruction that immediately follows an
unconditional jump is possible. This process eliminates a
sequence of instructions when repeated.
• Consider the following intermediate code representation:
if error == 1 goto L1
goto L2
L1: Print error information
L2:
• Here, the code is executed only if the variable error is equal to 1.
Peephole optimization allows the elimination of jumps over
jumps.
Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 5
Unreachable code elimination cont…
• Hence, the above code is replaced as follows irrespective of the
value of the variable debug.
if error ! = 1 goto L2
Print error information
L2:
• Now, if the value of the variable is set to 0, the code becomes:
if 0 != 1 goto L2
Print error information
L2:
• Here, the first statement always evaluates to true. Hence, the
statement printing the error information is unreachable and can
be eliminated.
Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 6
3. Flow-of-control Optimizations
• The peephole optimization helps to eliminate the unnecessary
jumps in the intermediate code.
• For example, consider the following code sequence:
goto L1

L1: goto L2
This sequence can be replaced by
goto L2

L1: goto L2

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 7


Flow-of-control Optimizations cont…
• Now, if there are no jumps to L1 and the statement L1: goto L2 is
preceded by an unconditional jump, then this statement can be
eliminated.
• Similarly, consider the following code sequence:
if (x < y) goto L1

L1: goto L2
• This sequence can be rewritten as follows:
if (x < y) goto L2

L1: goto L2

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 8


4. Algebraic Simplifications
• Algebraic identities can be used by a peephole optimizer to
eliminate three-address statements such as
x=x+0
or
x=x*1
in the peephole.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 9


5. Reduction-in-strength Transformations
• Peephole optimization also allows applying strength reduction
transformations to replace expensive operations by the
equivalent cheaper ones.
• For example, the expression X2 can be replaced by an equivalent
cheaper expression X * X.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 10


6. Use of Machine Idioms
• Some target machines provide hardware instructions to
implement certain operations in a better and efficient way.
• Thus, identifying the situations that permit the use of hardware
instructions to implement certain operations may reduce the
execution time significantly.
• For example, some machines provide auto-increment and auto-
decrement addressing modes, which add and subtract one
respectively from an operand.
• These modes can be used while pushing or popping a stack, or for
the statements of the form X = X+1 OR X = X-1. These
transformations greatly improve the quality of the code.
• For example INC i, (use to increase i by 1)

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 11


Thank You

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 12

You might also like