written 2.7 years ago by | • modified 2.7 years ago |
Definition:
Peephole optimisation is a simple and effective technique for locally improving target code. This technique is applied to improve the performance of the target program by examining the short sequence of target instructions (called the peephole) and replace these instructions replacing by shorter or faster sequence whenever possible. Peephole is a small, moving window on the target program.
Characteristics of Peephole Optimisation So The peephole optimisation can be applied to the target code using the following characteristic.
1. Redundant instruction elimination
- Especially the redundant loads and stores can be eliminated in this type of transformations.
Example: MOV R0,x MOV x,R0
- We can eliminate the second instruction since x is in already R0. But if MOV x, R0 is a label statement then we can not remove it.
2. Unreachable code
- Especially the redundant loads and stores can be eliminated in this type of transformations.
- An unlabeled instruction immediately following an unconditional jump may be removed.
- This operation can be repeated to eliminate the sequence of instructions.
Example:
# define debug 0
if(debug)
{
print debugging information
}
In the intermediate representation the if statement may be translated as if-
if debug=1 goto L1 goto L2 L1: print debugging information L2:
- Additionally, One obvious peephole optimization is to eliminate jumps over jumps. Thus no matter what the value of debugging, can replaced by: If debug goto L2debug≠1
Print debugging information
L2: - Now, since debug set to 0 at the beginning of the program, constant propagation program, should replace by
If 0≠1 goto L2≠1
Print debugging information
L2: - As the argument of the first statement of evaluates to a constant true, it can replace by goto L2. - Then all the statement that prints debugging aids are manifestly unreachable and can manifestly eliminate one at a time.
3. The flow of control optimization
- The unnecessary jumps can eliminate in either the intermediate code or the target code by the following types of peephole optimizations.
- We can replace the jump sequence.
Goto L1 …… L1: goto L2 By the sequence Goto L2 …….
L1: goto L2
- If there are no jumps to L1 then it may be possible to eliminate the statement L1: goto L2 provided it preceded by an unconditional jump. Similarly, the sequence
If a
……
L1: goto L2
Can replaced by
If a
……
L1: goto L2
4. Algebraic simplification
- So Peephole optimisation is an effective technique for algebraic simplification.
- The statements such as x = x + 0 or x := x* 1 can eliminated by peephole optimisation.
5. Reduction in strength
- Certain machine instructions are cheaper than the other.
- In order to improve the performance of the intermediate code, we can replace these instructions by equivalent cheaper instruction.
- So For example, x2 is cheaper than x * x. Similarly, addition and subtraction are cheaper than multiplication and division. So we can add an effectively equivalent addition and subtraction for multiplication and division.
6. Machine idioms
- So The target instructions have equivalent machine instructions for performing some have operations.
- Hence we can replace these target instructions by equivalent machine instructions in order to improve the efficiency.
- Example: Some machines have auto-increment or auto-decrement addressing modes.decrement These modes can use in a code for a statement like i=i+1.