CD Unit 5 1
CD Unit 5 1
COMPILER DESIGN
UNIT 5 – Code Optimization
Prepared by
Dr L Josephine Usha
AP/CSE
SRMIST
Unit V - Outline
Code Optimization
Basic Blocks and Flow Graphs
Sources of Optimization
• 1. Common Sub Expression Elimination
• 2. Copy Propagation
• 3. Dead-Code Elimination
• 4. Constant Folding
• 5. Loop Optimization
Flow graph for the three-address code fragment for quicksort. Each
Bi basic block.
Before After
• Delete any root from DAG that has no ancestors and is not live out (has
no live out variable associated).
• Repeat previous step till no change.
• Techniques that derive information about the flow of data along program
execution paths
• Examples
• One way to implement global common sub expression elimination requires us to
determine whether two identical expressions evaluate to the same value along any
possible execution path of the program.
• If the result of an assignment is not used along any subsequent execution path, then
we can eliminate the assignment as dead code.
• transfer function
• Both a and b will have the same value after the b=a statement.
• Transfer function of a statement s is denoted as fs
• Two flavors of transfer function
• Information propagate forward along execution paths
• Definitions:
1. defB: the set of variables defined in B prior to any use of that variable in B
2. useB: the set of variables whose values may be used in B prior to any definition of
the variable.
• Input
• Intermediate representation of source program
• Symbol Table – runtime addresses
• Intermediate Representation
• Linear rep – postfix notation
• Three address rep- quadruples
• Virtual rep- stack machine code
• Graphical rep – syntax trees and dags
• Prior to code generation
• Perform type checking
• Values can assigned to names
• Input should be free of errors.
5/3/2025 CS6660-Compiler Design 57
Target Programs
• Output
• Absolute machine language
• Output placed in fixed location in m/y, example PL/C
• Fast for small programs
• Relocatable machine language
• Output allows subprograms to be compiled separately
• Needs linker and loader
• Assembly code
• Use symbolic instructions and macro to generate code
• Needs assembler, linker and loader
• Postfix Notation
• Three-Address Code