OPTIMIZATION TECHNIQUES
PEEPHOLE OPTIMIZATION
TYPES OF OPTIMIZATION
Optimization can be categorized broadly into
two types :
1. Machine independent
2. Machine dependent
Optimized code,
• Improve the quality of the target code,
• Improve runtime or space
• Executes Faster
• Code size get reduced
• Efficient memory usage
Peep hole optimization
Peephole optimization-Replacing instruction
sequence within peephole by shorter or faster
sequence.
• Peephole is the machine dependent optimization
Objectives of Peephole Optimization
• The objective of peephole optimization is:
• To improve performance
• To reduce memory footprint
• To reduce code size
Characteristics of peephole optimization
• Redundant instruction elimination
• Unreachable code elimination
• Flow of control optimization
• Algebraic simplification
• Use of machine idioms
Redundant instruction elimination
• Reduces the instruction which is not necessary
loads and stores can be eliminated
eg: mov R0, x
mov x,R0
r2=r2+5 r2=r1+5
i=r2 r4=r2*3
r3=i
r4=r3*3
Example..
Int add_ten(int x) int add_ten(int x)
{ {
int y,z; int y;
y=10; y=10;
z=x+y; y=x+y;
return z; return y;
} }
Int add_ten(int x) int add_ten(int x)
{ {
int y=10; return x+10;
return x+y;
} }
Unreachable code elimination
• It is a part of program code that is never
accessed because of program constructs.
void add(int a, int b)
{ int c=a+b;
return c;
printf(“ %d”,c);-> unreachable
}
Flow of control optimization
• These are instances in a code where the program control jumps back and
forth without performing any significant task ,these jumps can be removed.
Mov R1,R2; MOV R1,R2;
GOTO L1; GOTO L2;
--------- ------------
L1:GOTO L2; ------------
--------- L2: INC R1;
L2:INC R1;
-------
• goto L1
L1: goto L2
-------
L2 : goto L3
-----------
L3: mov a, R0
Optimized code: goto L3
L3 : mov a,Ro
Algebraic simplification
• These are occasions where algebraic
expressions can be made simple
• X *2=X+X
• X=x+0 These kind of instructions
can be removed.
• X=x+1 this can simply replaced by inc x
Use of machine idioms
• Target instructions have equivalent machine
instructions for performing some operations
• X=x+1 x++
• X=x-1 x--