Principles of Code Optimization
Principles of Code Optimization
Artale (1)
Principle of Compilers
Lecture IX: Principles of Code
Optimization
Alessandro Artale
Faculty of Computer Science – Free University of Bolzano
Room: 221
[email protected]
http://www.inf.unibz.it/ artale/
Summary of Lecture IX
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (3)
Optimization never guarantees that the resulting code is the best possible.
The best transformations are those that yield the most benefit for the least
effort.
1. A transformation must preserve the meaning of a program. It’s better to
miss an opportunity to apply a transformation rather than risk changing
what the program does.
2. A transformation must, on the average, speed up a program by a
measurable amount.
3. Avoid code-optimization for programs that run occasionally or during
debugging.
4. Remember! Dramatic improvements are usually obtained by improving
the source code: The programmer is always responsible in finding the
best possible data structures and algorithms for solving a problem.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (5)
Quicksort: An Example Program
We will use the sorting program Quicksort to illustrate the effects of the
various optimization techniques.
void quicksort(m,n)
int m,n;
int i,j,v,x;
if (n <= m) return;
i = m-1; j = n; v = a[n]; /* fragment begins here */
while (1)
do i = i+1; while (a[i]<v);
do j = j-1; while (a[j]>v);
if (i>=j) break;
x = a[i]; a[i] = a[j]; a[j] =x;
Summary
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (8)
Flow graph for the three-address code fragment for quicksort. Each is a
basic block.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (10)
Summary
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (11)
have common subexpressions and can be eliminated.
After local common subexpression elimination, is transformed as:
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (13)
which are common subexpressions.
is evaluated in
by . Then, the statements
can be replaced by
the statements
can be replaced by
.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (14)
Common Subexpressions Elimination (Cont.)
Example. The following flow graph shows the result of eliminating both
local and global common subexpressions from basic blocks and .
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (15)
A DAG for a Basic Block has the following labels and nodes:
1. Leaves contain unique identifiers, either variable names or constants.
2. Interior nodes contain an operator symbol.
3. Nodes can optionally be associated to a list of variables representing
those variables having the value computed at the node.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (16)
The following shows both a three-address code of a basic block and its
associated DAG.
+ ,prod
prod
*
[] [] (1)
a + ,i
b * 20
4 i 1
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (17)
Summary
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (18)
Copy Propagation
Dead-Code Elimination
Example. Considering the Block after Copy Propagation we can see that
x is never reused all over the code. Thus, x is a dead variable and we can
eliminate the assignment from .
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (20)
Constant Folding
Example. Consider the conditional statement:
.
If, by Constant Folding, we discover that is always false we can eliminate
both the if-test and the jump to L.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (21)
Summary
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (22)
Loop Optimization
Code Motion
Reduction in Strength
in Block .
.
Thus, we may replace by
.
Problem: We need to initialize to before entering the Block .
Induction Variables
Example. Consider the loop of Bock . The variables j and are Induction
Variables. The same applies for variables i and in Block .
After Reduction in Strength is applied to both and , the only use of i and
j is to determine the test in .
Since and the test is equivalent to .
After this replacement in the test, both i (in Block ) and j (in Block )
become dead-variables and can be eliminated! (see next slide for the new
optimized code).
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (27)
Induction Variables Elimination: An Example (Cont.)
Flow Graph after Reduction in Strength and Induction-Variables elimination.
Free University of Bolzano–Principles of Compilers. Lecture IX, 2003/2004 – A.Artale (28)
Summary of Lecture IX
Code Optimization
Sources of Optimization
1. Common Subexpression Elimination
2. Copy Propagation
3. Dead-Code Elimination
4. Constant Folding
5. Loop Optimization