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

PPTX
Principal Sources of Optimization in compiler design
PPTX
Asymptotic Notation
PPTX
Intermediate code generator
PDF
Syntax Directed Definition and its applications
PPT
Chapter 5 Syntax Directed Translation
PPTX
Performance analysis(Time & Space Complexity)
PPTX
Bootstrapping in Compiler
PPTX
Loop optimization
Principal Sources of Optimization in compiler design
Asymptotic Notation
Intermediate code generator
Syntax Directed Definition and its applications
Chapter 5 Syntax Directed Translation
Performance analysis(Time & Space Complexity)
Bootstrapping in Compiler
Loop optimization

What's hot (20)

PPTX
Input-Buffering
PDF
Lecture 01 introduction to compiler
PPTX
Strassen's matrix multiplication
PPTX
Role-of-lexical-analysis
PPTX
Backtracking
PDF
Intermediate code generation in Compiler Design
PPTX
Principle source of optimazation
PPTX
Graph coloring using backtracking
PPTX
Type checking in compiler design
PPTX
Direct linking loaders
PPTX
Unit 3 sp assembler
PPT
Multi Head, Multi Tape Turing Machine
PDF
Target language in compiler design
PPTX
Register transfer language
PPTX
Data flow architecture
PPTX
Asymptotic notations
PPTX
Code Optimization
PPTX
Linear Block Codes
PPT
Logic Micro Operation
PPTX
Lexical analyzer generator lex
Input-Buffering
Lecture 01 introduction to compiler
Strassen's matrix multiplication
Role-of-lexical-analysis
Backtracking
Intermediate code generation in Compiler Design
Principle source of optimazation
Graph coloring using backtracking
Type checking in compiler design
Direct linking loaders
Unit 3 sp assembler
Multi Head, Multi Tape Turing Machine
Target language in compiler design
Register transfer language
Data flow architecture
Asymptotic notations
Code Optimization
Linear Block Codes
Logic Micro Operation
Lexical analyzer generator lex
Ad

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

PPTX
Generating code from dags
PPTX
Code generation
PPT
COMPILER_DESIGN_CLASS 2.ppt
PPTX
COMPILER_DESIGN_CLASS 1.pptx
PPT
Code Generations - 1 compiler design.ppt
PPTX
Compiler Design_Code generation techniques.pptx
PPT
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
PDF
Wondershare UniConverter Crack Download Latest 2025
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
PDF
Wondershare Filmora Crack 12.0.10 With Latest 2025
PDF
Skype 125.0.201 Crack key Free Download
PPT
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
PDF
The Inner Secrets of Compilers
PPTX
Bp150522
PPT
Lecture 16 17 code-generation
PPT
Code generator
PPT
dynamic programming Rod cutting class
Generating code from dags
Code generation
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 1.pptx
Code Generations - 1 compiler design.ppt
Compiler Design_Code generation techniques.pptx
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt
Wondershare UniConverter Crack Download Latest 2025
Enscape 3D 3.6.6 License Key Crack Full Version
Wondershare Filmora Crack 12.0.10 With Latest 2025
Skype 125.0.201 Crack key Free Download
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
The Inner Secrets of Compilers
Bp150522
Lecture 16 17 code-generation
Code generator
dynamic programming Rod cutting class
Ad

More from Dhrumil Panchal (20)

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

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PPTX
“Next-Gen AI: Trends Reshaping Our World”
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
436813905-LNG-Process-Overview-Short.pptx
PDF
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
TE-AI-Unit VI notes using planning model
PPTX
Glazing at Facade, functions, types of glazing
PPTX
anatomy of limbus and anterior chamber .pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Strings in CPP - Strings in C++ are sequences of characters used to store and...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Practice Questions on recent development part 1.pptx
Simulation of electric circuit laws using tinkercad.pptx
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
“Next-Gen AI: Trends Reshaping Our World”
Chapter 6 Design in software Engineeing.ppt
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
436813905-LNG-Process-Overview-Short.pptx
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
TE-AI-Unit VI notes using planning model
Glazing at Facade, functions, types of glazing
anatomy of limbus and anterior chamber .pptx
CH1 Production IntroductoryConcepts.pptx

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