0% found this document useful (0 votes)
82 views20 pages

5.4 Peep-Hole 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)
82 views20 pages

5.4 Peep-Hole 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/ 20

Module V:

Peep-hole Optimization

R19CS301 - Automata Theory and Session by:


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

20 November 2024 Peep-hole Optimization 1


2 20 November 2024

Agenda
➢ Introduction
➢ Key Features of Peep-hole Optimization
➢ Types of Peep-hole Optimizations
➢ Workflow of Peep-hole Optimization
➢ Advantages of Peep-hole Optimization
➢ Limitations of Peep-hole Optimization

Peep-hole 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

Peep-hole 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.

Peep-hole Optimization
5 20 November 2024

Peephole Optimization:
➢ Peep-hole optimization is a code optimization technique
performed on a small set of adjacent instructions in the
generated code.
➢ The goal is to identify and replace inefficient code patterns with
more efficient ones.
➢ This optimization is typically done in the final stages of
compilation, focusing on improving the performance and
reducing the size of the machine code.

Peep-hole Optimization
6 20 November 2024

Key Features of Peep-hole Optimization:


➢ Local Optimization: Peep-hole optimization examines a small
"window" or "peephole" of instructions at a time, rather than the
entire program.
➢ Pattern Matching: It identifies specific patterns of instructions that
can be optimized.
➢ Replacement: It replaces inefficient instruction sequences with
more efficient ones.

Peep-hole Optimization
7 20 November 2024

Types of Peep-hole Optimizations:


➢ Redundant Load and Store Elimination
➢ Unreachable Code Elimination
➢ Flow of Control Optimization
➢ Algebraic Simplification
➢ Strength Reduction
➢ Instruction Combination
➢ Instruction Elimination

Peep-hole Optimization
8 20 November 2024

1. Redundant Load and Store Elimination:


➢ This optimization removes unnecessary load and store
instructions that do not contribute to the program's final output.
➢ This reduces the number of memory accesses, improving
performance.
➢ Example:
MOV R1, A
MOV A, R1

Optimized Code:
// Redundant load and store removed
Peep-hole Optimization
9 20 November 2024

2. Unreachable Code Elimination:


➢ This optimization removes code that will never be executed,
such as instructions after a return statement or jump instruction.
This reduces code size and potential confusion.
➢ Example:
JMP L1
L2: NOP
L1: ADD R1, R2
Optimized Code:
JMP L1
L1: ADD R1, R2 // Unreachable code (L2: NOP) removed
Peep-hole Optimization
10 20 November 2024

3. Flow of Control Optimization:


➢ This optimization simplifies or eliminates unnecessary flow control
instructions, such as jumps to jumps, to streamline the control
flow of the program.
➢ Example:
JMP L1
L1: JMP L2

Optimized Code:
JMP L2 // Direct jump to L2

Peep-hole Optimization
11 20 November 2024

4. Algebraic Simplification:
➢ This optimization simplifies arithmetic expressions by applying
algebraic identities, reducing the number of operations
required.
➢ Example:
MUL R1, 2

Optimized Code:
ADD R1, R1
// Multiplication by 2 replaced with addition

Peep-hole Optimization
12 20 November 2024

5. Reduction in Strength:
➢ This optimization replaces expensive operations with equivalent
but less costly operations, such as replacing multiplication with
addition or shifting.
➢ Example:
MUL R1, 4

Optimized Code:
SHL R1, 2
// Multiplication by 4 replaced with shift left by 2

Peep-hole Optimization
13 20 November 2024

6. Instruction Combination:
➢ This optimization combines multiple instructions into a single instruction
to reduce the overall instruction count and improve execution speed.
➢ Example:
MOV R1, A
ADD R1, 1
MOV A, R1

Optimized Code:
INC A
// Combine move and add instructions into a single increment

Peep-hole Optimization
14 20 November 2024

7. Instruction Elimination:
➢ This optimization removes instructions that cancel each other out
or are unnecessary, streamlining the code.
➢ Example:
MOV R1, R2
MOV R2, R1

Optimized Code:
// No operation needed; instructions cancel each other
out

Peep-hole Optimization
15 20 November 2024

Workflow of Peep-hole Optimization:


➢ Scan: The optimizer scans a small window of instructions,
typically 2-3 adjacent instructions.
➢ Match: It matches the instructions against known patterns of
inefficient code.
➢ Replace: If a match is found, the optimizer replaces the
inefficient code with a more efficient sequence.
➢ Repeat: This process is repeated until the entire code has been
scanned and optimized.

Peep-hole Optimization
16 20 November 2024

Advantages of Peep-hole Optimization:


➢ Improves Performance: By replacing inefficient instructions with
more efficient ones.
➢ Reduces Code Size: By eliminating redundant and unnecessary
instructions.
➢ Simple and Localized: Focuses on small code segments, making
it simpler to implement and understand.

Peep-hole Optimization
17 20 November 2024

Limitations of Peep-hole Optimization:


➢ Local Scope: It only optimizes small sections of code, so it may
miss broader optimization opportunities.
➢ Limited by Patterns: It relies on predefined patterns, so it cannot
optimize code that does not match these patterns.

Peep-hole Optimization
18 20 November 2024

Peep-hole Optimization
19 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.
Peep-hole Optimization
20 20 November 2024

Peep-hole 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