5.4 Peep-Hole Optimization
5.4 Peep-Hole Optimization
Peep-hole Optimization
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
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
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
Peep-hole Optimization
7 20 November 2024
Peep-hole Optimization
8 20 November 2024
Optimized Code:
// Redundant load and store removed
Peep-hole Optimization
9 20 November 2024
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
Peep-hole Optimization
16 20 November 2024
Peep-hole Optimization
17 20 November 2024
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