SlideShare a Scribd company logo
4
Most read
5
Most read
9
Most read
Unlocking the Power of IR in
Compiler Construction
Outline:
1.Introduction to IR in Compiler Construction
2.Types of IR
3.Benefits of IR
4.Limitations of IR
5.Conclusion
6.References
Introduction to IR in Compiler Construction
Intermediate Representation (IR) is an important 4th phase of compiler
construction. It is a representation of the source code that is easier to process
and optimize than the original source code.
IR is generated by the frontend of a compiler. It is a language-independent
representation of the source code that can be used by the backend of the
compiler to generate the target code for a specific platform.
IR in Compiler Construction:
Source
Code
Front
End
IR Middle
End
IR Back
End
Target
Code
Properties of IR:
Some important IR properties are:
• Ease of generation
• Ease of manipulation
• Procedure size
• Freedom of expression
• Level of abstraction
Benefits of IR:
Intermediate Representations (IRs) have several benefits in compiler construction. Here are some
of the key benefits of using an IR:
1.Machine independence: IRs provide a machine-independent representation of the source code.
This allows the compiler to optimize and generate code that is specific to the target machine
without having to deal with the complexities of the source language.
2.Simplification of complex constructs: IRs can simplify complex constructs and expressions in
the source language. This can help reduce the complexity of the compiler and make it easier to
develop, maintain, and debug.
3.Optimization: IRs can be used to apply various optimization techniques to the code, such as
dead code elimination, constant folding, and loop unrolling. This can result in faster and more
efficient code.
Benefits of IR:
4.Modularity: The use of an IR can make the compiler more modular and easier to extend. The
front-end of the compiler can be designed to generate the IR, while the back-end can be designed to
generate code for different target machines.
5.Code generation: This allows the compiler to generate code that is optimized for the target
machine, without having to deal with the complexities of the source language.
6.Program analysis: IRs can be used for program analysis, such as detecting errors, checking for
security vulnerabilities, and measuring program complexity.
Types of IR:
There are many different types of IR, each designed for a specific purpose. The most common types of IR
are:
1.Abstract Syntax Tree (AST)
2.Directed Acyclic Graph(DAG)
3.Static Single Assignment(SSA)
4.Three-Address Code (TAC)
Abstract Syntax Tree:
ASTs are tree-like structures that capture the syntax of a program and provide a basis for analysis and
optimization.
ASTs are useful for a variety of compiler optimizations, including type checking, data flow analysis, and
code generation. They can be used to detect and report syntax errors, to analyze and transform the program
to improve its performance, and to generate efficient code for different target machines.
Examples:
Examples of ASTs range from simple arithmetic expressions to complex control-flow and data-flow graphs.
1. (a * b) + (c - d)
2. 4 * 7 + 3
Directed Acyclic Graph :
The Directed Acyclic Graph (DAG) is used to represent the structure of basic blocks, to visualize
the flow of values between basic blocks, and to provide optimization techniques in the basic block.
To apply an optimization technique to a basic block, a DAG is a three-address code that is
generated as the result of an intermediate code generation.
•The DAG-based IR represents the program as a directed acyclic graph, where each
node in the graph represents an operation, and each edge represents a dependency
between operations.
•The Directed Acyclic Graph (DAG) facilitates the transformation of basic blocks.
•DAG is an efficient method for identifying common sub-expressions.
Example:
•(b * c) - (b * c)
Static Single Assignment(SSA)
Static Single Assignment (SSA) is a means of structuring the IR (intermediate representation)
such that every variable is allotted a value only once and every variable is defined before it’s use.
In an SSA-based IR, each assignment statement creates a new variable, and each use of a variable
corresponds to a read from that variable. This allows the compiler to perform optimizations such as constant
propagation, dead code elimination, and loop-invariant code motion more easily,
Examples:
Convert the following code segment to SSA form:
a = q + d
q = b - c
q = a * q
Convert the following code segment to SSA form:
x = y - z s = x + s x = s + p s = z * q s = x * s
a = q + d q = b - c q = a * q
Three Address Code:
Three address code (TAC) is a low-level intermediate representation used by compilers to optimize and generate
machine code. TAC is typically used to represent complex expressions and statements in a simpler, machine-
independent format.
It is used to represent operations like if-else, while loop, and switch statements, allowing the compiler to
generate optimized code quickly and efficiently.
Examples:
(b + c) – (c + d)
t1= (b + c)
t2=(c + d)
t3=t1 – t2
TAC for Control Flow Statement
Three address code (TAC) is a low-level intermediate representation used by compilers to optimize and
generate machine code. TAC is typically used to represent complex expressions and statements in a simpler,
machine-independent format.
For control flow statements like switch statements, TAC can be used to represent the various cases and the
corresponding code blocks that are executed based on the value of the switch expression.
TAC for Switch statement:
Here is an example of TAC for a switch statement with three cases:
t1 = switch_expr
if t1 == 1 goto case1
if t1 == 2 goto case2
if t1 == 3 goto case3
goto default_case
case1:
// code for case 1
goto end_switch
case2:
// code for case 2
goto end_switch
TAC for Switch statement:
In this example, t1 is a temporary variable used to hold the value of the switch expression. The three if statements compare t1
to the case values, and the appropriate code block is executed if there is a match. If there is no match, the default_case block
is executed. Finally, the end_switch block contains any code that should be executed after the switch statement.
This TAC representation can be further optimized by the compiler to eliminate redundant checks and to generate more
efficient machine code.
Limitations of IR:
While Intermediate Representations (IRs) offer several advantages in compiler
construction, they also have some limitations. Here are some of the limitations of
IRs:
1.Limited expressiveness: The expressiveness of an IR is limited by its design
and purpose.
2.Increased complexity: A complex IR may require additional data structures and
algorithms to represent and manipulate the code, which can make the compiler
harder to understand, develop, and maintain.
Limitations of IR:
3.Performance overhead: The additional parsing and translation steps required to
convert the source code into an IR can slow down the compilation process and
increase the memory usage of the compiler.
4.Debugging difficulties: The IR may not be easily understandable or traceable,
making it harder to identify and fix errors in the code.
5.Portability issues: The portability of an IR can be a concern, as different
compilers may use different IRs, making it harder to share code between different
compilers or platforms.
References:
Compiler Construction. (n.d.). In Wikipedia. Retrieved July 6, 2020, from
https://en.wikipedia.org/wiki/Compiler_construction
Intermediate Representation. (n.d.). In Wikipedia. Retrieved July 6, 2020, from
https://en.wikipedia.org/wiki/Intermediate_representation
https://www.geeksforgeeks.org

More Related Content

PDF
Daa notes 3
PPTX
Graph coloring using backtracking
PPTX
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
PPTX
Principal Sources of Optimization in compiler design
PPTX
AI Programming language (LISP)
PDF
LR Parsing
PDF
Physical Layer Numericals - Data Communication & Networking
PPTX
Lexical analysis - Compiler Design
Daa notes 3
Graph coloring using backtracking
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Principal Sources of Optimization in compiler design
AI Programming language (LISP)
LR Parsing
Physical Layer Numericals - Data Communication & Networking
Lexical analysis - Compiler Design

What's hot (20)

PPTX
Top Down Parsing, Predictive Parsing
PPTX
NP completeness
PPTX
Rabin Karp ppt
PPT
Chapter 3 -Syntax Analyzer.ppt
PPTX
Dynamic Itemset Counting
PPTX
Regular Expression in Compiler design
PPTX
Type checking in compiler design
DOCX
Automata theory
PPT
14. Query Optimization in DBMS
PDF
Control Flow Analysis
PPTX
String Matching (Naive,Rabin-Karp,KMP)
PPTX
Local search algorithm
PPT
Code Optimization
PPTX
Stressen's matrix multiplication
PPT
Compiler Design
PPT
Sum of subsets problem by backtracking 
PPTX
Pumping lemma for regular set h1
PPTX
Peephole Optimization
PPTX
Lefmost rightmost TOC.pptx
PPTX
Parsing (Automata)
Top Down Parsing, Predictive Parsing
NP completeness
Rabin Karp ppt
Chapter 3 -Syntax Analyzer.ppt
Dynamic Itemset Counting
Regular Expression in Compiler design
Type checking in compiler design
Automata theory
14. Query Optimization in DBMS
Control Flow Analysis
String Matching (Naive,Rabin-Karp,KMP)
Local search algorithm
Code Optimization
Stressen's matrix multiplication
Compiler Design
Sum of subsets problem by backtracking 
Pumping lemma for regular set h1
Peephole Optimization
Lefmost rightmost TOC.pptx
Parsing (Automata)
Ad

Similar to Intermediate Representation in Compiler Construction (20)

PDF
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
PPTX
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
PPT
Data structure and algorithm.lect-03.ppt
PPTX
compiler design-Intermediate code generation.pptx
PPTX
Unit-2.pptx
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
PPT
A basic introduction to compiler design.ppt
PPT
A basic introduction to compiler design.ppt
PPTX
3 address code ujjwal matoliya.pptx
PPT
Compiler Construction introduction
PDF
Effisiensi prog atmel
PPTX
Improving Code Quality Through Effective Review Process
PPT
Cpcs302 1
PDF
Lecture 2.1 - Phase of a Commmmpiler.pdf
DOCX
Compiler Design Material
PPTX
Ch 1.pptx
DOCX
interview questions.docx
PDF
Phases of Compiler
PDF
Compiler gate question key
DOCX
AmiBroker AFL to DLL Conversion
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
Data structure and algorithm.lect-03.ppt
compiler design-Intermediate code generation.pptx
Unit-2.pptx
SPCC_Sem6_Chapter 6_Code Optimization part
A basic introduction to compiler design.ppt
A basic introduction to compiler design.ppt
3 address code ujjwal matoliya.pptx
Compiler Construction introduction
Effisiensi prog atmel
Improving Code Quality Through Effective Review Process
Cpcs302 1
Lecture 2.1 - Phase of a Commmmpiler.pdf
Compiler Design Material
Ch 1.pptx
interview questions.docx
Phases of Compiler
Compiler gate question key
AmiBroker AFL to DLL Conversion
Ad

Recently uploaded (20)

PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PDF
Digital Logic Computer Design lecture notes
PDF
Well-logging-methods_new................
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PPT on Performance Review to get promotions
PPTX
additive manufacturing of ss316l using mig welding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
Geodesy 1.pptx...............................................
Digital Logic Computer Design lecture notes
Well-logging-methods_new................
UNIT 4 Total Quality Management .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT on Performance Review to get promotions
additive manufacturing of ss316l using mig welding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

Intermediate Representation in Compiler Construction

  • 1. Unlocking the Power of IR in Compiler Construction
  • 2. Outline: 1.Introduction to IR in Compiler Construction 2.Types of IR 3.Benefits of IR 4.Limitations of IR 5.Conclusion 6.References
  • 3. Introduction to IR in Compiler Construction Intermediate Representation (IR) is an important 4th phase of compiler construction. It is a representation of the source code that is easier to process and optimize than the original source code. IR is generated by the frontend of a compiler. It is a language-independent representation of the source code that can be used by the backend of the compiler to generate the target code for a specific platform.
  • 4. IR in Compiler Construction: Source Code Front End IR Middle End IR Back End Target Code
  • 5. Properties of IR: Some important IR properties are: • Ease of generation • Ease of manipulation • Procedure size • Freedom of expression • Level of abstraction
  • 6. Benefits of IR: Intermediate Representations (IRs) have several benefits in compiler construction. Here are some of the key benefits of using an IR: 1.Machine independence: IRs provide a machine-independent representation of the source code. This allows the compiler to optimize and generate code that is specific to the target machine without having to deal with the complexities of the source language. 2.Simplification of complex constructs: IRs can simplify complex constructs and expressions in the source language. This can help reduce the complexity of the compiler and make it easier to develop, maintain, and debug. 3.Optimization: IRs can be used to apply various optimization techniques to the code, such as dead code elimination, constant folding, and loop unrolling. This can result in faster and more efficient code.
  • 7. Benefits of IR: 4.Modularity: The use of an IR can make the compiler more modular and easier to extend. The front-end of the compiler can be designed to generate the IR, while the back-end can be designed to generate code for different target machines. 5.Code generation: This allows the compiler to generate code that is optimized for the target machine, without having to deal with the complexities of the source language. 6.Program analysis: IRs can be used for program analysis, such as detecting errors, checking for security vulnerabilities, and measuring program complexity.
  • 8. Types of IR: There are many different types of IR, each designed for a specific purpose. The most common types of IR are: 1.Abstract Syntax Tree (AST) 2.Directed Acyclic Graph(DAG) 3.Static Single Assignment(SSA) 4.Three-Address Code (TAC)
  • 9. Abstract Syntax Tree: ASTs are tree-like structures that capture the syntax of a program and provide a basis for analysis and optimization. ASTs are useful for a variety of compiler optimizations, including type checking, data flow analysis, and code generation. They can be used to detect and report syntax errors, to analyze and transform the program to improve its performance, and to generate efficient code for different target machines. Examples: Examples of ASTs range from simple arithmetic expressions to complex control-flow and data-flow graphs. 1. (a * b) + (c - d) 2. 4 * 7 + 3
  • 10. Directed Acyclic Graph : The Directed Acyclic Graph (DAG) is used to represent the structure of basic blocks, to visualize the flow of values between basic blocks, and to provide optimization techniques in the basic block. To apply an optimization technique to a basic block, a DAG is a three-address code that is generated as the result of an intermediate code generation. •The DAG-based IR represents the program as a directed acyclic graph, where each node in the graph represents an operation, and each edge represents a dependency between operations. •The Directed Acyclic Graph (DAG) facilitates the transformation of basic blocks. •DAG is an efficient method for identifying common sub-expressions. Example: •(b * c) - (b * c)
  • 11. Static Single Assignment(SSA) Static Single Assignment (SSA) is a means of structuring the IR (intermediate representation) such that every variable is allotted a value only once and every variable is defined before it’s use. In an SSA-based IR, each assignment statement creates a new variable, and each use of a variable corresponds to a read from that variable. This allows the compiler to perform optimizations such as constant propagation, dead code elimination, and loop-invariant code motion more easily, Examples: Convert the following code segment to SSA form: a = q + d q = b - c q = a * q Convert the following code segment to SSA form: x = y - z s = x + s x = s + p s = z * q s = x * s a = q + d q = b - c q = a * q
  • 12. Three Address Code: Three address code (TAC) is a low-level intermediate representation used by compilers to optimize and generate machine code. TAC is typically used to represent complex expressions and statements in a simpler, machine- independent format. It is used to represent operations like if-else, while loop, and switch statements, allowing the compiler to generate optimized code quickly and efficiently. Examples: (b + c) – (c + d) t1= (b + c) t2=(c + d) t3=t1 – t2
  • 13. TAC for Control Flow Statement Three address code (TAC) is a low-level intermediate representation used by compilers to optimize and generate machine code. TAC is typically used to represent complex expressions and statements in a simpler, machine-independent format. For control flow statements like switch statements, TAC can be used to represent the various cases and the corresponding code blocks that are executed based on the value of the switch expression.
  • 14. TAC for Switch statement: Here is an example of TAC for a switch statement with three cases: t1 = switch_expr if t1 == 1 goto case1 if t1 == 2 goto case2 if t1 == 3 goto case3 goto default_case case1: // code for case 1 goto end_switch case2: // code for case 2 goto end_switch
  • 15. TAC for Switch statement: In this example, t1 is a temporary variable used to hold the value of the switch expression. The three if statements compare t1 to the case values, and the appropriate code block is executed if there is a match. If there is no match, the default_case block is executed. Finally, the end_switch block contains any code that should be executed after the switch statement. This TAC representation can be further optimized by the compiler to eliminate redundant checks and to generate more efficient machine code.
  • 16. Limitations of IR: While Intermediate Representations (IRs) offer several advantages in compiler construction, they also have some limitations. Here are some of the limitations of IRs: 1.Limited expressiveness: The expressiveness of an IR is limited by its design and purpose. 2.Increased complexity: A complex IR may require additional data structures and algorithms to represent and manipulate the code, which can make the compiler harder to understand, develop, and maintain.
  • 17. Limitations of IR: 3.Performance overhead: The additional parsing and translation steps required to convert the source code into an IR can slow down the compilation process and increase the memory usage of the compiler. 4.Debugging difficulties: The IR may not be easily understandable or traceable, making it harder to identify and fix errors in the code. 5.Portability issues: The portability of an IR can be a concern, as different compilers may use different IRs, making it harder to share code between different compilers or platforms.
  • 18. References: Compiler Construction. (n.d.). In Wikipedia. Retrieved July 6, 2020, from https://en.wikipedia.org/wiki/Compiler_construction Intermediate Representation. (n.d.). In Wikipedia. Retrieved July 6, 2020, from https://en.wikipedia.org/wiki/Intermediate_representation https://www.geeksforgeeks.org