5.3 Principal Sources of Optimization
5.3 Principal Sources of Optimization
Agenda
➢ Aim of Code Optimization
➢ Criteria for Code Improving Transformations
➢ Places for Improvement
➢ Constraints
➢ Principal Sources of Optimization
Course Outcomes:
CO # Outcome K Level
Design and analyze Finite Automata and regular expression for any given Regular
CO1 K3
language
Understand and apply concepts of Context-Free Grammars, Push-Down Automata,
CO2 K3
and Turing machines to analyze computational problems and language ambiguities.
Demonstrate proficiency in Lexical Analysis, including defining tokens, recognizing
CO3 K2
tokens, and utilizing tools like Lex
Design efficient code generators and optimize code using techniques like peep-hole
CO5 K2
optimization, DAGs, and basic block optimization.
Constraints:
Some languages may not permit certain code
improvements
➢ Array accesses in C vs FORTRAN
Compiler must use the machine’s resources efficiently
➢ Registers to accommodate heavily used variables
➢ Use a variety of Addressing modes
1. Constant Folding:
➢ Constant folding is an optimization that evaluates constant
expressions at compile time rather than at runtime.
➢ This reduces the number of calculations the program must perform
during execution.
➢ Example:
int x = 3 + 4;
// The compiler replaces 3 + 4 with 7 at compile time.
3. Code Motion:
➢ Code motion, also known as loop-invariant code motion, moves
code outside of loops if it does not change within the loop. This
reduces the number of instructions executed within the loop.
➢ Example:
for (int i = 0; i < 100; i++) {
int j = 5; // j is not changed inside loop
printf("%d\n", i * j); }
// Optimized Code:
int j = 5; // j moved outside loop
for (int i = 0; i < 100; i++) {
printf("%d\n", i * j); } Principal Sources of Optimization
13 20 November 2024
4. Strength Reduction:
➢ Strength reduction replaces expensive operations with cheaper
ones. For example, multiplication can be replaced with addition in
loops.
➢ Example:
for (int i = 0; i < 100; i++) {
a[i] = i * 4; // replace Multiplication with addition
}
// Optimized Code:
for (int i = 0; i < 100; i++) {
a[i] = i + i + i + i; // i * 4 replaced with i+i+i+i
}
Principal Sources of Optimization
14 20 November 2024
5. Inline Expansion:
➢ Inline expansion replaces a function call with the body of the
function. This reduces the overhead of function.
➢ Example:
int square(int x) {
return x * x;
}
int result = square(5); // Function call.
//Optimized Code:
int result = 5 * 5; // Inline expansion.
Principal Sources of Optimization
15 20 November 2024
6. Loop Optimization:
➢ Loop optimizations improve the performance of loops. Techniques
include loop unrolling, loop fusion, and loop interchange.
➢ Example:
for (int i = 0; i < 100; i++) {
a[i] = i; }
// Optimized Code (loop Unroll):
for (int i = 0; i < 100; i += 4) {
a[i] = i;
a[i + 1] = i + 1;
a[i + 2] = i + 2;
a[i + 3] = i + 3; } Principal Sources of Optimization
16 20 November 2024
7. Register Allocation:
➢ Register allocation efficiently uses CPU registers to store frequently
accessed variables, reducing the need to access slower memory.
➢ Example:
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i]; }
// Optimized:
register int sum = 0;
for (register int i = 0; i < n; i++) {
sum += a[i]; }
Principal Sources of Optimization
17 20 November 2024
8. Peephole Optimization:
➢ Makes small, localized changes to a sequence of instructions to
improve performance. It looks at a small "window" or "peephole" of
instructions and makes improvements.
➢ Example:
MOV R0, #5
MOV R1, #0
ADD R1, R0
// Optimized Code:
MOV R1, #5
// Optimized Code:
int temp = b * c;
int a = temp + d;
int e = temp + f; // b * c is computed once and reused.
Principal Sources of Optimization
19 20 November 2024
// Optimized Code:
int result = 5 + 3; // Function inlined.
References
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman,
“Compilers: Principles, Techniques and Tools”, Second Edition,
Pearson Education, 2009.
Steven S. Muchnick, “Advanced Compiler Design and
Implementation”, Morgan Kaufmann Publishers - Elsevier Science,
India, Indian Reprint 2003.
V. Raghavan, “Principles of Compiler Design”, Tata McGraw Hill
Education Publishers, 2010.
Allen I. Holub, “Compiler Design in C”, Prentice-Hall Software
Series, 1993.
Randy Allen, Ken Kennedy, “Optimizing Compilers for Modern
Architectures: A Dependence based Approach”, Morgan
Kaufmann Publishers, 2002.
Principal Sources of Optimization
22 20 November 2024