SlideShare a Scribd company logo
3
Most read
5
Most read
7
Most read
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Dynamic Programming
Code-Generation
Algorithm
 The dynamic programming algorithm proceeds
in three phases:
1. Compute bottom-up for each node n in the
expression tree T an array C of costs, in which
the ith component C[i] is the optional cost of
computing the sub-tree S rooted at n into a
register, assuming i registers are available for
the computation, for 1<= i <= r.
1. Mentioned in previous slide
2. Traverse T, using the cost vectors to determine
which sub-trees of T must ne computed into
memory.
3. Traverse each tree using the cost vectors and
associated instructions to generate the final
target code. The code for the sub-trees
computed into memory locations is generated
first.
 Consider a machine having two registers R0 and
R1, and the following instructions, each of unit
cost:
LD Ri, Kj // Ri = Mj
op Ri, Ri, Ri // Ri = Ri Op Rj
op Ri, Ri, Mi // Ri = Ri Op Kj
LD Ri, Ri // Ri = Ri
ST Hi, Ri // Mi = Rj
 In these instructions, Ri is either R0 or R1, and Mi is
a memory location. The operator op corresponds
to an arithmetic operators.
 Let us apply the dynamic programming algorithm to generate
optimal code for the syntax tree in Fig 8.26.
 In the first phase, we compute the cost vectors shown at each
node.
 To illustrate this cost computation, consider the cost vector at
the leaf a.
 C[0], the cost of computing a into memory, is 0 since it is
already there.
 C[l], the cost of computing a into a register, is 1 since we can
load it into a register with the instruction LD R0, a.
 C[2], the cost of loading a into a register with two registers
available, is the same as that with one register available.
 The cost vector at leaf a is therefore (0,1,1).
 Consider the cost vector at the root.
 We first determine the minimum cost
of computing the root with one and
two registers available.
 The machine instruction ADD R0, R0,
M matches the root, because the root
is labeled with the operator +.
 Using this instruction, the minimum
cost of evaluating the root with one
register available is the minimum cost
of computing its right subtree into
memory, plus the minimum cost of
computing its left subtree into the
register, plus 1 for the instruction.
 No other way exists. The cost vectors
at the right and left children of the
root show that the minimum cost of
computing the root with one register
available is 5 + 2 + 1 = 8.
 Now consider the minimum cost of evaluating the
root with two registers available. Three cases arise
depending on which instruction is used to compute
the root and in what order the left and right sub-
trees of the root are evaluated.
 Compute the left sub-tree with two registers
available into register R0, compute the right sub-
tree with one register available into
register R1, and use the instruction ADD R0, R0,
R1 to compute the root. This sequence has cost 2 +
5 + 1 = 8.
 Compute the right sub-tree with two registers
available into R1, compute the left sub-tree with
one register available into R0, and use the
instruction ADD R0, R0, R1. This sequence has cost
4 + 2 + 1 = 7.
 Compute the right sub-tree into memory
location M, compute the left sub-tree with two
registers available into register R0, and use the
instruction ADD R0, R0, M. This sequence has cost
5 + 2 + 1 = 8.
 The second choice gives the minimum cost 7.
 The minimum cost of computing the root into
memory is determined by adding one to the
minimum cost of computing the root with all
registers avail-able; that is, we compute the
root into a register and then store the result.
The cost vector at the root is therefore (8,8,7).
 From the cost vectors we can easily construct
the code sequence by making a traversal of the
tree. From the tree in Fig. 8.26, assuming two
registers are available, an optimal code
sequence is
LD R0, c // R0 = c
LD R1, d // R1 = d
DIV R1, R1, e // R1 = R1 / e
MUL R0, R0, R1 // R0 = R0 * R1
LD R1, a // R1 = a
SUB R1, R1, b // R1 = R1 - b
ADD R1, R1, R0 // R1 = R1 + R0
 Dynamic programming techniques have been used
in a number of compilers, including the second
version of the portable C compiler, PCC2 . The
technique facilitates retargeting because of the
applicability of the dynamic programming
technique to a broad class of machines.
 Inspiration from Prof. Nidhi Shah
 Notes of CD
 Textbook of CD
 Images from Google Images
Dynamic Programming Code-Optimization Algorithm (Compiler Design)

More Related Content

What's hot (20)

PPTX
Model Based Software Architectures
Munazza-Mah-Jabeen
 
PPTX
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
PDF
Storage organization and stack allocation of space
Muhammad Haroon
 
PPTX
Link state routing protocol
Aung Thu Rha Hein
 
DOCX
Nonrecursive predictive parsing
alldesign
 
KEY
Testing Hadoop jobs with MRUnit
Eric Wendelin
 
PPT
KNOWLEDGE REPRESENTATION ISSUES.ppt
SuneethaChittineni
 
PPTX
Introduction to loaders
Tech_MX
 
PPTX
Entity Relationship design issues
Megha Sharma
 
PPTX
Assemblers
Dattatray Gandhmal
 
PPTX
source code metrics and other maintenance tools and techniques
Siva Priya
 
PPT
02. chapter 3 lexical analysis
raosir123
 
PPTX
The analysis synthesis model of compilation
Huawei Technologies
 
PPT
Taxonomy for bugs
Harika Krupal
 
PPTX
Issues in design_of_code_generator
vinithapanneer
 
PDF
DBMS LECTURE NOTES FOR AKTU
Sunit Mishra
 
PDF
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
PPT
POST’s CORRESPONDENCE PROBLEM
Rajendran
 
PPTX
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
PPTX
Artifacts
Mayuresh Wadekar
 
Model Based Software Architectures
Munazza-Mah-Jabeen
 
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
Storage organization and stack allocation of space
Muhammad Haroon
 
Link state routing protocol
Aung Thu Rha Hein
 
Nonrecursive predictive parsing
alldesign
 
Testing Hadoop jobs with MRUnit
Eric Wendelin
 
KNOWLEDGE REPRESENTATION ISSUES.ppt
SuneethaChittineni
 
Introduction to loaders
Tech_MX
 
Entity Relationship design issues
Megha Sharma
 
Assemblers
Dattatray Gandhmal
 
source code metrics and other maintenance tools and techniques
Siva Priya
 
02. chapter 3 lexical analysis
raosir123
 
The analysis synthesis model of compilation
Huawei Technologies
 
Taxonomy for bugs
Harika Krupal
 
Issues in design_of_code_generator
vinithapanneer
 
DBMS LECTURE NOTES FOR AKTU
Sunit Mishra
 
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
POST’s CORRESPONDENCE PROBLEM
Rajendran
 
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
Artifacts
Mayuresh Wadekar
 

Similar to Dynamic Programming Code-Optimization Algorithm (Compiler Design) (20)

PPTX
Generating code from dags
indhu mathi
 
PPTX
Code generation
Aparna Nayak
 
PPT
COMPILER_DESIGN_CLASS 2.ppt
ssuserebb9821
 
PPTX
COMPILER_DESIGN_CLASS 1.pptx
ssuserebb9821
 
PPT
Code Generations - 1 compiler design.ppt
SreepriyaPilla
 
PPTX
Compiler Design_Code generation techniques.pptx
RushaliDeshmukh2
 
PPT
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
boyingbo
 
PDF
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
PDF
Wondershare Filmora Crack 12.0.10 With Latest 2025
alihamzakpa010
 
PDF
Internet Download Manager (IDM) 6.42.27 Crack Latest 2025
umnazadiwe
 
PDF
Skype 125.0.201 Crack key Free Download
alihamzakpa015
 
PDF
Wondershare Recoverit Crack for MacOS Full Download (Latest 2025)
abidkhan77g77
 
PPT
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
sneharaju2025
 
PDF
Download GRAPHISOFT ArchiCAD Crack 28.1.0.4001 Full Version 2025
am2612067
 
PDF
New Wondershare UniConverter Crack Free Download (Latest 2025)
am2612067
 
PDF
The Inner Secrets of Compilers
IT MegaMeet
 
Generating code from dags
indhu mathi
 
Code generation
Aparna Nayak
 
COMPILER_DESIGN_CLASS 2.ppt
ssuserebb9821
 
COMPILER_DESIGN_CLASS 1.pptx
ssuserebb9821
 
Code Generations - 1 compiler design.ppt
SreepriyaPilla
 
Compiler Design_Code generation techniques.pptx
RushaliDeshmukh2
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
456589.-Compiler-Design-Code-Generation (1).ppt
boyingbo
 
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
Wondershare Filmora Crack 12.0.10 With Latest 2025
alihamzakpa010
 
Internet Download Manager (IDM) 6.42.27 Crack Latest 2025
umnazadiwe
 
Skype 125.0.201 Crack key Free Download
alihamzakpa015
 
Wondershare Recoverit Crack for MacOS Full Download (Latest 2025)
abidkhan77g77
 
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
sneharaju2025
 
Download GRAPHISOFT ArchiCAD Crack 28.1.0.4001 Full Version 2025
am2612067
 
New Wondershare UniConverter Crack Free Download (Latest 2025)
am2612067
 
The Inner Secrets of Compilers
IT MegaMeet
 
Ad

More from Dhrumil Panchal (20)

PPTX
YouTube Cryptocurrency Scam
Dhrumil Panchal
 
PPTX
This and Static Keyword
Dhrumil Panchal
 
PPTX
Servlet and Servlet Life Cycle
Dhrumil Panchal
 
PPTX
Properties and Indexers
Dhrumil Panchal
 
PPTX
Chomsky Normal Form
Dhrumil Panchal
 
PPTX
IEEE 802.11 Architecture and Services
Dhrumil Panchal
 
PPTX
Key roles for successful analytic project in Data Mining
Dhrumil Panchal
 
PPTX
Different Software Testing Types and CMM Standard
Dhrumil Panchal
 
PPTX
Web Design Issues
Dhrumil Panchal
 
PPTX
Toy Interpreter
Dhrumil Panchal
 
PPTX
Traditional Problems Associated with Computer Crime
Dhrumil Panchal
 
PPTX
Breadth First Search (BFS)
Dhrumil Panchal
 
PPTX
Timing Diagram of MVI Instruction of 8085 Microprocessor
Dhrumil Panchal
 
PPTX
File Management – File Concept, access methods, File types and File Operation
Dhrumil Panchal
 
PPTX
Constructor and Types of Constructors
Dhrumil Panchal
 
PPTX
Types of Instruction Format
Dhrumil Panchal
 
PPTX
Types of Cables(Guided Media for Transmisson)
Dhrumil Panchal
 
PPTX
Global Service for Mobile Communication
Dhrumil Panchal
 
PPTX
Denial of Service Attack
Dhrumil Panchal
 
PPTX
Fourier Series
Dhrumil Panchal
 
YouTube Cryptocurrency Scam
Dhrumil Panchal
 
This and Static Keyword
Dhrumil Panchal
 
Servlet and Servlet Life Cycle
Dhrumil Panchal
 
Properties and Indexers
Dhrumil Panchal
 
Chomsky Normal Form
Dhrumil Panchal
 
IEEE 802.11 Architecture and Services
Dhrumil Panchal
 
Key roles for successful analytic project in Data Mining
Dhrumil Panchal
 
Different Software Testing Types and CMM Standard
Dhrumil Panchal
 
Web Design Issues
Dhrumil Panchal
 
Toy Interpreter
Dhrumil Panchal
 
Traditional Problems Associated with Computer Crime
Dhrumil Panchal
 
Breadth First Search (BFS)
Dhrumil Panchal
 
Timing Diagram of MVI Instruction of 8085 Microprocessor
Dhrumil Panchal
 
File Management – File Concept, access methods, File types and File Operation
Dhrumil Panchal
 
Constructor and Types of Constructors
Dhrumil Panchal
 
Types of Instruction Format
Dhrumil Panchal
 
Types of Cables(Guided Media for Transmisson)
Dhrumil Panchal
 
Global Service for Mobile Communication
Dhrumil Panchal
 
Denial of Service Attack
Dhrumil Panchal
 
Fourier Series
Dhrumil Panchal
 
Ad

Recently uploaded (20)

PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 

Dynamic Programming Code-Optimization Algorithm (Compiler Design)

  • 3.  The dynamic programming algorithm proceeds in three phases: 1. Compute bottom-up for each node n in the expression tree T an array C of costs, in which the ith component C[i] is the optional cost of computing the sub-tree S rooted at n into a register, assuming i registers are available for the computation, for 1<= i <= r.
  • 4. 1. Mentioned in previous slide 2. Traverse T, using the cost vectors to determine which sub-trees of T must ne computed into memory. 3. Traverse each tree using the cost vectors and associated instructions to generate the final target code. The code for the sub-trees computed into memory locations is generated first.
  • 5.  Consider a machine having two registers R0 and R1, and the following instructions, each of unit cost: LD Ri, Kj // Ri = Mj op Ri, Ri, Ri // Ri = Ri Op Rj op Ri, Ri, Mi // Ri = Ri Op Kj LD Ri, Ri // Ri = Ri ST Hi, Ri // Mi = Rj  In these instructions, Ri is either R0 or R1, and Mi is a memory location. The operator op corresponds to an arithmetic operators.
  • 6.  Let us apply the dynamic programming algorithm to generate optimal code for the syntax tree in Fig 8.26.  In the first phase, we compute the cost vectors shown at each node.  To illustrate this cost computation, consider the cost vector at the leaf a.  C[0], the cost of computing a into memory, is 0 since it is already there.  C[l], the cost of computing a into a register, is 1 since we can load it into a register with the instruction LD R0, a.  C[2], the cost of loading a into a register with two registers available, is the same as that with one register available.  The cost vector at leaf a is therefore (0,1,1).
  • 7.  Consider the cost vector at the root.  We first determine the minimum cost of computing the root with one and two registers available.  The machine instruction ADD R0, R0, M matches the root, because the root is labeled with the operator +.  Using this instruction, the minimum cost of evaluating the root with one register available is the minimum cost of computing its right subtree into memory, plus the minimum cost of computing its left subtree into the register, plus 1 for the instruction.  No other way exists. The cost vectors at the right and left children of the root show that the minimum cost of computing the root with one register available is 5 + 2 + 1 = 8.
  • 8.  Now consider the minimum cost of evaluating the root with two registers available. Three cases arise depending on which instruction is used to compute the root and in what order the left and right sub- trees of the root are evaluated.  Compute the left sub-tree with two registers available into register R0, compute the right sub- tree with one register available into register R1, and use the instruction ADD R0, R0, R1 to compute the root. This sequence has cost 2 + 5 + 1 = 8.
  • 9.  Compute the right sub-tree with two registers available into R1, compute the left sub-tree with one register available into R0, and use the instruction ADD R0, R0, R1. This sequence has cost 4 + 2 + 1 = 7.  Compute the right sub-tree into memory location M, compute the left sub-tree with two registers available into register R0, and use the instruction ADD R0, R0, M. This sequence has cost 5 + 2 + 1 = 8.  The second choice gives the minimum cost 7.
  • 10.  The minimum cost of computing the root into memory is determined by adding one to the minimum cost of computing the root with all registers avail-able; that is, we compute the root into a register and then store the result. The cost vector at the root is therefore (8,8,7).  From the cost vectors we can easily construct the code sequence by making a traversal of the tree. From the tree in Fig. 8.26, assuming two registers are available, an optimal code sequence is
  • 11. LD R0, c // R0 = c LD R1, d // R1 = d DIV R1, R1, e // R1 = R1 / e MUL R0, R0, R1 // R0 = R0 * R1 LD R1, a // R1 = a SUB R1, R1, b // R1 = R1 - b ADD R1, R1, R0 // R1 = R1 + R0  Dynamic programming techniques have been used in a number of compilers, including the second version of the portable C compiler, PCC2 . The technique facilitates retargeting because of the applicability of the dynamic programming technique to a broad class of machines.
  • 12.  Inspiration from Prof. Nidhi Shah  Notes of CD  Textbook of CD  Images from Google Images