0% found this document useful (0 votes)
70 views

5.3 Principal Sources of Optimization

Uploaded by

siva.e2022cse
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)
70 views

5.3 Principal Sources of Optimization

Uploaded by

siva.e2022cse
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/ 22

Module V:

Principal Sources of Optimization

R19CS301 - Automata Theory and Session by:


Compiler Design Dr. S. Sampath Kumar, ASP/CSE

20 November 2024 Principal Sources of Optimization 1


2 20 November 2024

Agenda
➢ Aim of Code Optimization
➢ Criteria for Code Improving Transformations
➢ Places for Improvement
➢ Constraints
➢ Principal Sources of Optimization

Principal Sources of Optimization


3 20 November 2024

Module V: Intermediate Code, Code Generation and


Code Optimization
Issues in Code Generation - Design of a simple Code
Generator - Principal Sources of Optimization – Peep-
hole optimization – DAG - Optimization of Basic Blocks

Principal Sources of Optimization


4 20 November 2024

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

CO4 Analyze and construct different parsers K3

Design efficient code generators and optimize code using techniques like peep-hole
CO5 K2
optimization, DAGs, and basic block optimization.

Principal Sources of Optimization


5 20 November 2024

Aim of Code Optimization:


➢ Optimization techniques aim to improve the performance and
efficiency of the generated code by reducing execution time,
memory usage, and power consumption.
➢ They are applied at different stages of the compilation process to
ensure that the final executable is as efficient as possible.

Principal Sources of Optimization


6 20 November 2024

Criteria for Code Improving Transformations:


➢ Meaning Preserving
➢ Speedup program considerably
➢ Must be worth the effort

Principal Sources of Optimization


7 20 November 2024

Places for Improvement:


➢ Source Code: User can profile program, change algorithm,
transform loops
➢ Intermediate code: Compiler can improve loops, procedure calls,
address calculations
➢ Target code: Compiler can use registers, select instructions,
peephole transformations

Principal Sources of Optimization


8 20 November 2024

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

Principal Sources of Optimization


9 20 November 2024

Principal Sources of Optimization:


Optimization refers to the process of improving the performance
and efficiency of the generated code.
➢ Constant Folding
➢ Dead Code Elimination
➢ Code Motion
➢ Strength Reduction
➢ Inline Expansion
➢ Loop Optimization
➢ Register Allocation
➢ Peephole Optimization
➢ Common Subexpression Elimination
Principal Sources of Optimization
➢ Procedure Integration
10 20 November 2024

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.

Principal Sources of Optimization


11 20 November 2024

2. Dead Code Elimination:


➢ Dead code elimination removes code that does not affect the
program's output, such as variables that are never used or
statements that are never executed.
➢ Example:
int x = 5;
int y = 10;
return x; // y is never used and can be eliminated.

Principal Sources of Optimization


12 20 November 2024

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

Principal Sources of Optimization


18 20 November 2024

9. Common Subexpression Elimination:


➢ Identify and reuse expressions that are computed multiple times.
This reduces redundant calculations.
➢ Example:
int a = b * c + d;
int e = b * c + f; // b * c is computed twice.

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

10. Procedure Integration:


➢ Procedure integration, also known as function inlining, combines
smaller procedures into a single procedure to reduce the
overhead of procedure calls.
➢ Example:
void add(int x, int y) {
return x + y; }

// Optimized Code:
int result = 5 + 3; // Function inlined.

Principal Sources of Optimization


20 20 November 2024

Principal Sources of Optimization


21 20 November 2024

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

Principal Sources of Optimization

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy