2. Embedded and Parallel Systems Lab2
The Structure of Recent
Front end per
language
High-level
optimizations
Global optimizer
Code generator
Intermediate
representation
Dependencies Function
Language Dependent machine
independent
Transform language to
common intermediate form
Largely machine independent For example, procedure
inlineing (also called
procedure intergration)
Small language dependencies
machine dependencies slight
(e.g., register count/types)
Including global and local
optimizations + register
allocation
Highly machine dependent
Language independent
Detailed instruction selection
and machine-dependent
optimizations
May include or be followed
by assembler
4. Embedded and Parallel Systems Lab4
Procedure integration
Replace procedure call by procedure body
Int a;
void up(){
a=a+1;
}
Void main(){
a=10;
up();
b=a+5;
}
Int a;
Void main(){
a=10;
a=a+1;
b=a+5;
}
5. Embedded and Parallel Systems Lab5
Common subexpression elimination
Replace two instances of same
computation by single copy
a = b * c + g;
d = b * c * d;
tmp = b * c;
a = tmp + g;
d = tmp * d;
6. Embedded and Parallel Systems Lab6
Constant propagation
Replace all instances of a variable that is
assigned a constant with the constant
int x = 14;
int y = 7 - x / 2;
return y * (28 / x + 2);
int x = 14;
int y = 7 - 14 / 2;
return y * (28 / 14 +
2);
int x = 14;
int y = 0;
return y * 4;
7. Embedded and Parallel Systems Lab7
Stack height reduction
Rearrange expression tree to minimize resources needed for
expression evaluation
ADD R6,R2,R3
ADD R7,R6,R4
ADD R8,R7,R5
ADD R6,R2,R3
ADD R7,R4,R5
ADD R8,R7,R6
I1
I2
I3
I1 I2
I3
R8=((R2+R3)+R4)+R5 R8=(R2+R3)+(R4+R5)
8. Embedded and Parallel Systems Lab8
Copy propagation
Replace all instances of a variable A that
has been assigned X (i.e., A = X) with X
y = x ;
z = 3 + y;
z = 3 + x
9. Embedded and Parallel Systems Lab9
Code motion
Remove code from a loop that computes
same value each iteration of the loop
Loop-invariant code
while (j < maximum - 1) {
x=1;
j = j + 4 * a;
}
int maxval = maximum - 1;
int calcval = 4 * a;
x=1;
while (j < maxval) {
j = j + calcval;
}
10. Embedded and Parallel Systems Lab10
Induction variable elimination
Simplify / eliminate array addressing
calculations within loops
Int i=0;
while( i<10){
i=i+1;
p = 4*i ;
do some things;
}
Int p=0;
while( p<40){
p=p+4;
do some things;
}
11. Embedded and Parallel Systems Lab11
Strength reduction
Such as , replace multiply by constant
with adds and shifts
for (i=0 ; i<n ; i++){
z = i * x;
do some things;
}
for (i=0 ; i<n ; i++){
z = z + x;
do some things;
}
12. Embedded and Parallel Systems Lab12
Branch offset optimization
Choose the shortest branch displacement
that reaches target
if( a ){
statement 1
}
else {
goto L1;
}
statement 2
L1:
statement 3
if( !a ){
goto L1;
}
statement 1
statement 2
L1:
statement 3
13. Embedded and Parallel Systems Lab13
dead (unreachable) code elimination
Remove instructions that will not affect the
behavior of the program.
Int func( int a){
int b, c;
b=a*2;
c=a*3;
return b;
}
Int func( int a){
int b, c;
b=a*2;
return b;
}
15. Embedded and Parallel Systems Lab15
Loop unrolling
int i;
for (i = 0; i < 5; i++){
function(i);
}
function(0);
function(1);
function(2);
function(3);
function(4);
Duplicates the body of the loop multiple times, in order to decrease
the number of times the loop condition is tested and the number of
jumps, which hurt performance by impairing the instruction pipeline