SlideShare a Scribd company logo
UNIT V
CODE OPTIMIZATION
CODE OPTIMIZATION
 Optimization is a program transformation technique,
which tries to improve the code by making it consume less
resources (i.e. CPU, Memory) and deliver high speed.
 Optimization can be categorized broadly into two types :
1. machine independent and
2. machine dependent.
Organization of the Code Optimizer
The Principal Sources of Optimization
 A transformation of a program is called local if it can
be performed by looking only at the statements in a
basic block, otherwise it is called global.
Function Preserving Transformations:
 Common Sub expression Elimination
 Copy Propagation
 Dead-Code Elimination
 Constant Folding
An ‘occurrence’ of an expression E is called a common
subexpression if E was previously computed and the
values of variable in E have not changed since the
previous computation
Flow graph before and after eliminating the
local common sub expressions in B2
Copy Propagation
Copy propagation transformation is to use ‘y’ for ‘x’ wherever
possible after the copy statement x := y.
For example copy propagation applied to following block B
B2
x := t3
a[t4] := x
goto B2
yields the resultant block, as follows:
B2
x := t3
a[t4] := t3
goto B2
Dead Code Elimination
A variable is live at a point in a program if its value can be
used subsequently otherwise it is dead at that point
For Example: Dead code elimination removes the assignment
B2
x := t3
a[t4] := x
goto B2
Into
B2
a[t4] := t3
goto B2
 In this technique, it involves eliminating the
dead code.
 The statements of the code which either never
executes or are unreachable or their output is
never used are eliminated.
Example-
Reduction in strength
 It replaces expensive operation by a cheaper one
such as multiplication by an addition.
 For Example: The block B in the following flow
graph.
Thus the resultant flow graph after reduction in strength
appears as follows:
 In this technique, it involves reducing the
strength of expressions.
 This technique replaces the expensive and costly
operators with the simple and cheaper ones.
Example-
Loop Optimizations
 Running time of a program may be improved if
we decrease the number of instructions in an
inner loop, even if we increase the amount of
code outside that loop.
 Three techniques are important for loop
optimizataion
1. Code Motion
2. Induction Variable Elimination
3. Reduction in Strength
Code Optimization Techniques-
BASIC BLOCKS
 Source codes which are always executed in sequence
are considered as the basic blocks of the code.
 These basic blocks do not have any jump statements
among them
 A program can have various constructs as basic
blocks, like IF-THEN-ELSE,
 SWITCH-CASE conditional statements and
 loops such as DO-WHILE, FOR, and REPEAT-
UNTIL, etc.
Basic block identification
 Search header statements of all the basic blocks
from where a basic block starts:
 First statement of a program.
 Statements that are target of any branch
(conditional/unconditional).
 Statements that follow any branch statement.
 Header statements and the statements following
them form a basic block.
 A basic block does not include any header
statement of any other basic block.
UNIT V - Compiler Design notes power point presentation
DAG representation of Basic Blocks
 Directed a cyclic graphs (dags) are useful data
structures for implementing transformation on
basic blocks.
 The Dag for the statement x := y + z is:
The Dag for the statements.
t1:= 4 * i
t2:= a[t ] is
Peephole Optimization
 Techniques for “locally improving” the largest
code is peephole optimization
 Method for trying to improve the performance of
the target program by examining a short
sequence of target instructions (called the
peephole)
 replacing these instructions by a shorter or faster
sequence hidden ever possible.
Characteristics of Peephole Optimizations:
 Redundant instruction elimination
 Flow of control optimizations
 Algebraic simplifications
 Use of machine idioms.
Optimization of Basic Blocks
Code improving transformations for basic blocks
includes:
1. Common subexpression elimination
2. Dead code elimination
3. Reduction in strength.
Many of the structure preserving transformation
can be implemented by constructing a dag for a
basic block.
Common Subexpression Elimination
 The dag for the following block:
 a := b + c
 b := a - d
 c := b + c
 d := a - d is shown as follows:
If b is not live on exit we can eliminate the
common subexpressions and transform the
block as follows:
a := b + c
d := a - d
c := d + c
Corresponding dag is:
BASIC BLOCKS AND FLOW GRAPHS
 A graph representation of 3 address statements
is called a flow graph.
 Nodes in the flow graph represent computations
and the edges represents the flow of control.
Basic Blocks
 A basic block is a sequence of consecutive
statements in which flow of control enters at the
beginning and leaves at the end without halt or
possibility of branching except at the end.
 The following sequence of three address,
statements forms a basic block:
t:= a * a
t:= a * b
t:= 2 * t
t:= t+ t
Algorithm
Partition into basic blocks
Input: A sequence of three address statements.
Output: A list of basic blocks with each three
address statement in exactly one block.
Source code to compute the sum of 2 arrays
begin
sum := 0
i := 1
do begin
sum := sum + a[i] + b[i];
i = i + 1;
end
While i < - 10
end
A list of 3 address statements is:
1) sum := 0
2) i = 1
3) t1 := 4 * i
4) t2 := a[t1]
5) t3 := 4 * i
6) t4 := b[t3]
7) t5 := t2 + t4
8) t6 := sum + t5
9) sum := t6
10) t7 := i + 1
11) i = t7
12) i < = 10 goto 3
Basic Blocks
Transformations on Basic Blocks
Two important classes of local transformations
that can be applied to basic blocks are
 Structure preserving transformations and
 Algebraic transformation.
Structure Preserving Transformations:
The primary structure preserving transformations
are:
 Common subexpression elimination
 Dead code elimination
 Renaming of temporary variables
 Intercharge of two independent adjacent
statements
Common Subexpression Elimination
Consider the basic block
a := b + c
d := b + c.
As both the statements performs the same
computation in the block, it can be transformed
into an equivalent block as shown next
a := b + c
d := a.
The Principal Sources of Optimization
Types:
1. Function Preserving Transformations
2. Loop Optimizations
1. Function Preserving Transformations
 Common Sub expression Elimination
 Copy Propagation
 Dead-Code Elimination
 Constant Folding
2. Loop Optimizations
 Code Motion
 Induction Variable Elimination
 Reduction in Strength
Peephole Optimizations
1. Common subexpression elimination
2. Dead code elimination
3. Reduction in strength.
Basic Blocks
1. Structure preserving transformations
 Common sub expression elimination
 Dead code elimination
 Renaming of temporary variables
 Intercharge of two independent adjacent statements
2. Algebraic transformation.

More Related Content

Similar to UNIT V - Compiler Design notes power point presentation (20)

PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PPTX
Basic blocks and control flow graphs
Tilakpoudel2
 
PPTX
Code Optimization
Akhil Kaushik
 
PDF
Code optimization in compiler design
Kuppusamy P
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PPT
lect23_optimization.ppt
ssuser0be977
 
PPTX
Basic blocks - compiler design
hmnasim15
 
PPTX
Principle source of optimazation
Siva Sathya
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
Optimization
Royalzig Luxury Furniture
 
PPTX
Bp150520
saraswathi saraswathi
 
PPTX
Principal source of optimization in compiler design
Rajkumar R
 
PPT
Code Generations - 1 compiler design.ppt
SreepriyaPilla
 
PPTX
Compiler Design_Code generation techniques.pptx
RushaliDeshmukh2
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PPTX
Compiler optimization techniques
Hardik Devani
 
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
NiramayKolalle
 
PPT
COMPILER_DESIGN_CLASS 2.ppt
ssuserebb9821
 
PPTX
COMPILER_DESIGN_CLASS 1.pptx
ssuserebb9821
 
basics of optimizations presentation s
AnkitKumarSharma26
 
complier design unit 5 for helping students
aniketsugandhi1
 
Basic blocks and control flow graphs
Tilakpoudel2
 
Code Optimization
Akhil Kaushik
 
Code optimization in compiler design
Kuppusamy P
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
lect23_optimization.ppt
ssuser0be977
 
Basic blocks - compiler design
hmnasim15
 
Principle source of optimazation
Siva Sathya
 
Principal source of optimization in compiler design
Rajkumar R
 
Code Generations - 1 compiler design.ppt
SreepriyaPilla
 
Compiler Design_Code generation techniques.pptx
RushaliDeshmukh2
 
Optimization in Programming languages
Ankit Pandey
 
Compiler optimization techniques
Hardik Devani
 
SPCC_Sem6_Chapter 6_Code Optimization part
NiramayKolalle
 
COMPILER_DESIGN_CLASS 2.ppt
ssuserebb9821
 
COMPILER_DESIGN_CLASS 1.pptx
ssuserebb9821
 

Recently uploaded (20)

PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Design Thinking basics for Engineers.pdf
CMR University
 
Ad

UNIT V - Compiler Design notes power point presentation

  • 2. CODE OPTIMIZATION  Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed.  Optimization can be categorized broadly into two types : 1. machine independent and 2. machine dependent.
  • 3. Organization of the Code Optimizer
  • 4. The Principal Sources of Optimization  A transformation of a program is called local if it can be performed by looking only at the statements in a basic block, otherwise it is called global. Function Preserving Transformations:  Common Sub expression Elimination  Copy Propagation  Dead-Code Elimination  Constant Folding
  • 5. An ‘occurrence’ of an expression E is called a common subexpression if E was previously computed and the values of variable in E have not changed since the previous computation
  • 6. Flow graph before and after eliminating the local common sub expressions in B2
  • 7. Copy Propagation Copy propagation transformation is to use ‘y’ for ‘x’ wherever possible after the copy statement x := y. For example copy propagation applied to following block B B2 x := t3 a[t4] := x goto B2 yields the resultant block, as follows: B2 x := t3 a[t4] := t3 goto B2
  • 8. Dead Code Elimination A variable is live at a point in a program if its value can be used subsequently otherwise it is dead at that point For Example: Dead code elimination removes the assignment B2 x := t3 a[t4] := x goto B2 Into B2 a[t4] := t3 goto B2
  • 9.  In this technique, it involves eliminating the dead code.  The statements of the code which either never executes or are unreachable or their output is never used are eliminated. Example-
  • 10. Reduction in strength  It replaces expensive operation by a cheaper one such as multiplication by an addition.  For Example: The block B in the following flow graph.
  • 11. Thus the resultant flow graph after reduction in strength appears as follows:
  • 12.  In this technique, it involves reducing the strength of expressions.  This technique replaces the expensive and costly operators with the simple and cheaper ones. Example-
  • 13. Loop Optimizations  Running time of a program may be improved if we decrease the number of instructions in an inner loop, even if we increase the amount of code outside that loop.  Three techniques are important for loop optimizataion 1. Code Motion 2. Induction Variable Elimination 3. Reduction in Strength
  • 15. BASIC BLOCKS  Source codes which are always executed in sequence are considered as the basic blocks of the code.  These basic blocks do not have any jump statements among them  A program can have various constructs as basic blocks, like IF-THEN-ELSE,  SWITCH-CASE conditional statements and  loops such as DO-WHILE, FOR, and REPEAT- UNTIL, etc.
  • 16. Basic block identification  Search header statements of all the basic blocks from where a basic block starts:  First statement of a program.  Statements that are target of any branch (conditional/unconditional).  Statements that follow any branch statement.  Header statements and the statements following them form a basic block.  A basic block does not include any header statement of any other basic block.
  • 18. DAG representation of Basic Blocks  Directed a cyclic graphs (dags) are useful data structures for implementing transformation on basic blocks.  The Dag for the statement x := y + z is:
  • 19. The Dag for the statements. t1:= 4 * i t2:= a[t ] is
  • 20. Peephole Optimization  Techniques for “locally improving” the largest code is peephole optimization  Method for trying to improve the performance of the target program by examining a short sequence of target instructions (called the peephole)  replacing these instructions by a shorter or faster sequence hidden ever possible.
  • 21. Characteristics of Peephole Optimizations:  Redundant instruction elimination  Flow of control optimizations  Algebraic simplifications  Use of machine idioms.
  • 22. Optimization of Basic Blocks Code improving transformations for basic blocks includes: 1. Common subexpression elimination 2. Dead code elimination 3. Reduction in strength. Many of the structure preserving transformation can be implemented by constructing a dag for a basic block.
  • 23. Common Subexpression Elimination  The dag for the following block:  a := b + c  b := a - d  c := b + c  d := a - d is shown as follows:
  • 24. If b is not live on exit we can eliminate the common subexpressions and transform the block as follows: a := b + c d := a - d c := d + c Corresponding dag is:
  • 25. BASIC BLOCKS AND FLOW GRAPHS  A graph representation of 3 address statements is called a flow graph.  Nodes in the flow graph represent computations and the edges represents the flow of control.
  • 26. Basic Blocks  A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end.  The following sequence of three address, statements forms a basic block: t:= a * a t:= a * b t:= 2 * t t:= t+ t
  • 27. Algorithm Partition into basic blocks Input: A sequence of three address statements. Output: A list of basic blocks with each three address statement in exactly one block.
  • 28. Source code to compute the sum of 2 arrays begin sum := 0 i := 1 do begin sum := sum + a[i] + b[i]; i = i + 1; end While i < - 10 end
  • 29. A list of 3 address statements is: 1) sum := 0 2) i = 1 3) t1 := 4 * i 4) t2 := a[t1] 5) t3 := 4 * i 6) t4 := b[t3] 7) t5 := t2 + t4 8) t6 := sum + t5 9) sum := t6 10) t7 := i + 1 11) i = t7 12) i < = 10 goto 3
  • 31. Transformations on Basic Blocks Two important classes of local transformations that can be applied to basic blocks are  Structure preserving transformations and  Algebraic transformation.
  • 32. Structure Preserving Transformations: The primary structure preserving transformations are:  Common subexpression elimination  Dead code elimination  Renaming of temporary variables  Intercharge of two independent adjacent statements
  • 33. Common Subexpression Elimination Consider the basic block a := b + c d := b + c. As both the statements performs the same computation in the block, it can be transformed into an equivalent block as shown next a := b + c d := a.
  • 34. The Principal Sources of Optimization Types: 1. Function Preserving Transformations 2. Loop Optimizations 1. Function Preserving Transformations  Common Sub expression Elimination  Copy Propagation  Dead-Code Elimination  Constant Folding 2. Loop Optimizations  Code Motion  Induction Variable Elimination  Reduction in Strength
  • 35. Peephole Optimizations 1. Common subexpression elimination 2. Dead code elimination 3. Reduction in strength.
  • 36. Basic Blocks 1. Structure preserving transformations  Common sub expression elimination  Dead code elimination  Renaming of temporary variables  Intercharge of two independent adjacent statements 2. Algebraic transformation.