0% found this document useful (0 votes)
67 views19 pages

Of The Text Book: Code Optimization

The document discusses various code optimization techniques including constant folding, induction variable elimination, common subexpression elimination, copy propagation, dead code elimination, loop optimizations like loop unrolling and jamming. The goal of code optimization is to improve performance by maximizing speed and minimizing memory usage through techniques that detect patterns and replace them with more efficient constructs during compiler optimizations at both the intermediate and machine-dependent levels.

Uploaded by

zemike
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)
67 views19 pages

Of The Text Book: Code Optimization

The document discusses various code optimization techniques including constant folding, induction variable elimination, common subexpression elimination, copy propagation, dead code elimination, loop optimizations like loop unrolling and jamming. The goal of code optimization is to improve performance by maximizing speed and minimizing memory usage through techniques that detect patterns and replace them with more efficient constructs during compiler optimizations at both the intermediate and machine-dependent levels.

Uploaded by

zemike
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/ 19

Chapter 10

of the Text book


Code Optimization
• A main goal is to achieve a better performance
– Maximize execution speed and minimize memory usage
• Code optimization are generally applied after syntax
analyzers, usually both before and during code
generation.
– Add operation takes less time than multiplication operation.

source
Front End Intermediate Code Gen target
Code
Code Code

user Machine-
independent Machine-
Compiler dependent
optimizer Compiler
optimizer
• The technique consists of detecting
patterns and replacing them with efficient
constructs. The richest source of
optimization is the efficient usage of
registers and instruction set of a machine.
• After register utilization, the next important
one to consider towards optimization is
handling inner-loop
• Rule: 90 – 10 refers 90% of the time is
spent in 10% of the code
Optimization Techniques
1. Constant folding
A=I+J+B A = T1 + B
T1 = I + J
C= I+J+C C = T1 + C
2. Induction variables
For (i=0; I < 20; i++) A = 0;
{ For (i=0; I < 20; ++i)
{
A = I * 4; A = I + 4;
} }
Multiplication is replaced with less expensive addition
3. Repositioning Loop Variants

i = 0;
I = 0;
do { A = B;
P = P * I; do {
A = B; P = P * I;
I++; I++;
} while(I < 20);
} while(I < 20);

A = B has no role in the loop, but ssignment


performed 20 times.
• Two levels

– Machine independent code opt


• Control Flow analysis
• Data Flow analysis
• Transformation

– Machine dependent code-op


• Register allocation in optimal way
• Utilization of special instructions that minimize time
Basic Block
• BB is a sequence of consecutive
statements in which the flow control enters
at the beginning and leaves at the end w/o
halt or possible branching except at the
end
Basic Block
Principal sources of optimization
• Local optimization: within a basic block
• Global optimization: otherwise
• Mixed
Function-Preserving
Transformation
• Improving performance w/o changing fn.
• Techniques
– Common subexpression Elimination
– Copy Propagation
– Dead-code elimination
– Constant folding
Common subexpression
Elimination
• An occurrence of an expression E is
common subexpression if E was
previously computed and the values of
variables in E have not changed since.
Copy Propagation
• An idea behind this technique is to use g for f
whenever possible after the copy of
f := g
before After
x := t3 x := t3
a[t7] := t5 a[t7] := t5
a[t10] := x a[t10] := t3
Goto b2 Goto b2
x = 3;
y = x + 4;
Below is the code fragment after constant
propagation and constant folding.
x = 3;
y = 7;
Dead code elimination
• Remove unreachable code

• If (debug) print …

• Many times, debug := false


Remove codes no more important or
executable
Loop optimizations
• Beyond basic block
• Three important techniques
– Code motion
– Induction-variable elimination
– Reduction in strength
Code motion
• Move code outside the loop since there
are potential many iterations
• Look for expressions that yeild the same
result independent of the iterations.

• before after
• While ( I <= limit – 2). T = limit – 2
• While ( I <= t)
Induction-variable elimination &
Reduction in strength
• Look for induction variables for strength
reductions
• E.g. a pattern of changes in a lock step

B3: B3:
j = j - 1 j = j - 1
t4 = 4 *j t4 = t4 -4
t5 = a [ t4] t5 = a [ t4]
If t5 > v goto B3 If t5 > v goto B3
Loop Unrolling
Avoids a test at every iteration by recognizing that the
number of iterations is constant and replicating the body of
the loop. But NOT applicable to all.

J =0;
For (J=0; J < 100; ++J) while (J < 99)
{ {
A[J] = 5; A[J] = 5 ;
A[J+1] = 5 ;
} J+=2;
}
LOOP JAMMING
To merge body of two or more loops. Its necessary, each loop should be
executed the same number of tiems and that the indices are same.
Applicable to some indices of same value, NOT applicable to all

For (I=0; I < 10; ++I)


{ For (I=0; I < 10; ++I)
For (J=0; J < 20; ++J) {
{ For (J=0; J < 20; ++J)
ARRAY[I][J] = 0; {
} ARRAY[I][J] = 0;
} ARRAY[I][I] = 1;
For (I=0; I < 20; ++I) }
{ }
ARRAY[I][I] = 1;
}
Refer other optimizations
• Optimizations for Basic blocks
• Reducible flow graph
• Global Data flow analysis
• Machine dependent Optimizations

You might also like