SlideShare a Scribd company logo
BASIC BLOCKS AND CONTROL
FLOW GRAPHS
Presented By:
Tilak Poudel
Nepathya College
6th sem
2075/08/16/01
Basic Blocks and Control Flow Graphs
Basic Blocks
Introduction:
■ A basic block is a sequence of consecutive instructions which are
always executed in sequence without halt or possibilty of branching.
■ The basic block doesnot have any jump statements among them.
Basic Blocks and Control Flow Graphs
Basic Blocks
■ When the first instruction is executed , all the instructions in the same basic block will be
executed in their sequence of appearance without losing the flow control from the program.
Basic Blocks Construction Algorithm
■ Input: A sequence of three-address statements.
■ Output: A list of basic blocks with each three-address statement in exactly one
block.
Basic Blocks Construction Algorithm
1. Determine the set of leaders,
a. The first statement is the leader.
b. Any statement that is the target of a conditional or goto is a leader.
c. Any statement that immediately follows conditional or goto is a leader.
2. For each leader, its basic block consist of the leader and all statements up to but
not including the next leader or the end of the program
Basic Blocks Construction Example
Consider the following three address code statements .Compute the basic blocks.
1) PROD = 0
2) I = 1
3) T2=addr(A)-4
4) T4 =addr(B)-4
5) T1 =4*I
6) T3= T2[T1]
7) T5 = T4[T1]
8) T6 =T3*T5
9) PROD =PROD + T6
10) I =I+1
11) IF I<= 20 GOTO (5)
Basic Blocks Construction Example
Solution:
■ Because first statement is a leader ,so –
PROD =0 is a leader.
■ Because the target statement of conditional or unconditional goto statement is a
leader, so-
T1 =4*I is also a leader
Basic Blocks Construction Example
So the given code can be portioned in to 2 blocks as :
B1:
B2:
PROD = 0
I = 1
T2=addr(A)-4
T4 =addr(B)-4
T1 =4*I
T3= T2[T1]
T5 = T4[T1]
T6 =T3*T5
PROD =PROD + T6
I =I+1
IF I<= 20 GOTO B2
Basic Blocks and Control Flow Graphs
Flow Graphs
Introduction:
■ A flow graph is a graphical representation of a sequence of instructions with
control flow edges.
■ A flow graph can be defined at the intermediate code level or target code level.
■ The nodes of flow graphs are the basic blocks and flow-of-control to immediately
follow node connected by directed arrow.
Flow Graphs
Points to remember:
■ The basic blocks are the nodes to the flow graph .
■ The block whose leader is the first statement is called
initial block.
■ There is a directed edge from block B1 to B2 if B2
immediately follows B1 in the given sequence
■ Then we say that B1 is the predecessor of B2.
Flow Graphs
The flow graph for the above three address code is given below:
B1:
B2:
PROD = 0
I = 1
T2=addr(A)-4
T4 =addr(B)-4
T1 =4*I
T3= T2[T1]
T5 = T4[T1]
T6 =T3*T5
PROD =PROD + T6
I =I+1
IF I<= 20 GOTO B2
Equivalence of Basic Blocks
■ Two basic blocks are (semantically) equivalent if they compute the same set of
expressions
Transformation:
a := c*a a := c*a
b := 0 b := 0
■ Blocks are equivalent, assuming t1 and t2 are dead: no longer used (no longer live)
b := 0
t1 := a + b
t2 := c * t1
a := t2
a := c * a
b := 0
Transformations on Basic Blocks
■ A code-improving transformation is a code optimization to improve speed or
reduce code size.
■ Global transformations are performed across basic blocks.
■ Local transformations are only performed on single basic blocks.
■ Transformations must be safe and preserve the meaning of the code .
■ A local transformation is safe if the transformed basic block is guaranteed to be
equivalent to its original form
Transformations on Basic Blocks
Some local transformation are:
LocalTransformation
Common-
Subexpression
Elimination
Dead Code Elimination
Renaming Temporary
Variables
Interchange of
Statements
Algebraic
Transformations
Common-Subexpression Elimination
■ Remove redundant computations
■ 2nd and 4th:compute same expression in fig:1(a)
■ Look at 1st and 3rd in fig:1(b) :b is redefine in 2nd therefore different in3rd, not the same
expression
fig:1(a) fig:1(b)
a := b + c
b := a - d
c := b + c
d := a - d
a := b + c
b := a - d
c := b + c
d := b
t1 := b * c
t2 := a - t1
t3 := b * c
t4 := t2 + t3
t1 := b * c
t2 := a - t1
t4 := t2 + t1
Dead Code Elimination
■ Remove unused statements
Assuming a is dead (not used)
Remove unreachable code
b := a + 1
a := b + c
…
b := a + 1
…
goto L2
b := x + y
…
Renaming Temporary Variables
■ Temporary variables that are dead at the end of a block can be safely renamed.
■ The basic block is transforms into an equivalent block in which each statement that
defines a temporary defines a new temporary.
■ Such a basic block is called normal-form block or simple block.
Normal-form block
■ Note that normal-form blocks permit all statement interchanges that are possible.
t1 := b + c
t2 := a - t1
t1 := t1 * d
d := t2 + t1
t1 := b + c
t2 := a - t1
t3 := t1 * d
d := t2 + t3
Interchange of Statements
■ Independent statements can be reordered without effecting the value of block to
make its optimal use.
■ Note that normal-form blocks permit all statement interchanges that are possible
t1 := b + c
t2 := a - t1
t3 := t1 * d
d := t2 + t3
t1 := b + c
t3 := t1 * d
t2 := a - t1
d := t2 + t3
Algebraic Transformations
■ Change arithmetic operations to transform blocks to algebraic equivalent forms.
■ Simplify expression or replace expensive expressions by cheaper ones.
■ In statement 3,usually require a function call
■ Transforms to simple and equivalent statement
t1 := a - a
t2 := b + t1
t3 := t2 **2
t1 := 0
t2 := b
t3 := t2 * t2
!! Queries ??
Q & A
Thank You 

More Related Content

What's hot

Run time storage
Run time storageRun time storage
Run time storage
Rasineni Madhan Mohan Naidu
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Three Address code
Three Address code Three Address code
Three Address code
Pooja Dixit
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
Mohammad Vaseem Akaram
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
United International University
 
Address in the target code in Compiler Construction
Address in the target code in Compiler ConstructionAddress in the target code in Compiler Construction
Address in the target code in Compiler Construction
Muhammad Haroon
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
Jothi Lakshmi
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
sanchi29
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 

What's hot (20)

Run time storage
Run time storageRun time storage
Run time storage
 
Code generation
Code generationCode generation
Code generation
 
Three Address code
Three Address code Three Address code
Three Address code
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Address in the target code in Compiler Construction
Address in the target code in Compiler ConstructionAddress in the target code in Compiler Construction
Address in the target code in Compiler Construction
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 

Similar to Basic blocks and control flow graphs

Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecture
Prashant Singh
 
Basic Block
Basic BlockBasic Block
Basic Block
Shiv1234567
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
LogsAk
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
Jena Catherine Bel D
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
daimk2020
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
daimk2020
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
JohnSamuel280314
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
ssuser0be977
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
ishwarya516
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
BBDITM LUCKNOW
 
lect23_optimization.ppt
lect23_optimization.pptlect23_optimization.ppt
lect23_optimization.ppt
ssuser0be977
 
Instruction_Set.pdf
Instruction_Set.pdfInstruction_Set.pdf
Instruction_Set.pdf
boukomra
 
Instruction set
Instruction setInstruction set
Instruction set
Lívia Sousa
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
bhavanatmithun
 

Similar to Basic blocks and control flow graphs (20)

Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecture
 
Bp150520
Bp150520Bp150520
Bp150520
 
Basic Block
Basic BlockBasic Block
Basic Block
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Lecture03
Lecture03Lecture03
Lecture03
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
lect23_optimization.ppt
lect23_optimization.pptlect23_optimization.ppt
lect23_optimization.ppt
 
Instruction_Set.pdf
Instruction_Set.pdfInstruction_Set.pdf
Instruction_Set.pdf
 
Instruction set
Instruction setInstruction set
Instruction set
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
 
Ch9b
Ch9bCh9b
Ch9b
 

More from Tilakpoudel2

Motivation in software project development
Motivation in software project developmentMotivation in software project development
Motivation in software project development
Tilakpoudel2
 
SAX
SAXSAX
Advantages and disadvantages of Simulation
Advantages and disadvantages of SimulationAdvantages and disadvantages of Simulation
Advantages and disadvantages of Simulation
Tilakpoudel2
 
Output analysis for simulation models / Elimination of initial Bias
Output analysis for simulation models / Elimination of initial BiasOutput analysis for simulation models / Elimination of initial Bias
Output analysis for simulation models / Elimination of initial Bias
Tilakpoudel2
 
Expert system
Expert systemExpert system
Expert system
Tilakpoudel2
 
Markov chain and its Application
Markov chain and its Application Markov chain and its Application
Markov chain and its Application
Tilakpoudel2
 

More from Tilakpoudel2 (6)

Motivation in software project development
Motivation in software project developmentMotivation in software project development
Motivation in software project development
 
SAX
SAXSAX
SAX
 
Advantages and disadvantages of Simulation
Advantages and disadvantages of SimulationAdvantages and disadvantages of Simulation
Advantages and disadvantages of Simulation
 
Output analysis for simulation models / Elimination of initial Bias
Output analysis for simulation models / Elimination of initial BiasOutput analysis for simulation models / Elimination of initial Bias
Output analysis for simulation models / Elimination of initial Bias
 
Expert system
Expert systemExpert system
Expert system
 
Markov chain and its Application
Markov chain and its Application Markov chain and its Application
Markov chain and its Application
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 

Basic blocks and control flow graphs

  • 1. BASIC BLOCKS AND CONTROL FLOW GRAPHS Presented By: Tilak Poudel Nepathya College 6th sem 2075/08/16/01
  • 2. Basic Blocks and Control Flow Graphs Basic Blocks Introduction: ■ A basic block is a sequence of consecutive instructions which are always executed in sequence without halt or possibilty of branching. ■ The basic block doesnot have any jump statements among them.
  • 3. Basic Blocks and Control Flow Graphs Basic Blocks ■ When the first instruction is executed , all the instructions in the same basic block will be executed in their sequence of appearance without losing the flow control from the program.
  • 4. Basic Blocks Construction Algorithm ■ Input: A sequence of three-address statements. ■ Output: A list of basic blocks with each three-address statement in exactly one block.
  • 5. Basic Blocks Construction Algorithm 1. Determine the set of leaders, a. The first statement is the leader. b. Any statement that is the target of a conditional or goto is a leader. c. Any statement that immediately follows conditional or goto is a leader. 2. For each leader, its basic block consist of the leader and all statements up to but not including the next leader or the end of the program
  • 6. Basic Blocks Construction Example Consider the following three address code statements .Compute the basic blocks. 1) PROD = 0 2) I = 1 3) T2=addr(A)-4 4) T4 =addr(B)-4 5) T1 =4*I 6) T3= T2[T1] 7) T5 = T4[T1] 8) T6 =T3*T5 9) PROD =PROD + T6 10) I =I+1 11) IF I<= 20 GOTO (5)
  • 7. Basic Blocks Construction Example Solution: ■ Because first statement is a leader ,so – PROD =0 is a leader. ■ Because the target statement of conditional or unconditional goto statement is a leader, so- T1 =4*I is also a leader
  • 8. Basic Blocks Construction Example So the given code can be portioned in to 2 blocks as : B1: B2: PROD = 0 I = 1 T2=addr(A)-4 T4 =addr(B)-4 T1 =4*I T3= T2[T1] T5 = T4[T1] T6 =T3*T5 PROD =PROD + T6 I =I+1 IF I<= 20 GOTO B2
  • 9. Basic Blocks and Control Flow Graphs Flow Graphs Introduction: ■ A flow graph is a graphical representation of a sequence of instructions with control flow edges. ■ A flow graph can be defined at the intermediate code level or target code level. ■ The nodes of flow graphs are the basic blocks and flow-of-control to immediately follow node connected by directed arrow.
  • 10. Flow Graphs Points to remember: ■ The basic blocks are the nodes to the flow graph . ■ The block whose leader is the first statement is called initial block. ■ There is a directed edge from block B1 to B2 if B2 immediately follows B1 in the given sequence ■ Then we say that B1 is the predecessor of B2.
  • 11. Flow Graphs The flow graph for the above three address code is given below: B1: B2: PROD = 0 I = 1 T2=addr(A)-4 T4 =addr(B)-4 T1 =4*I T3= T2[T1] T5 = T4[T1] T6 =T3*T5 PROD =PROD + T6 I =I+1 IF I<= 20 GOTO B2
  • 12. Equivalence of Basic Blocks ■ Two basic blocks are (semantically) equivalent if they compute the same set of expressions Transformation: a := c*a a := c*a b := 0 b := 0 ■ Blocks are equivalent, assuming t1 and t2 are dead: no longer used (no longer live) b := 0 t1 := a + b t2 := c * t1 a := t2 a := c * a b := 0
  • 13. Transformations on Basic Blocks ■ A code-improving transformation is a code optimization to improve speed or reduce code size. ■ Global transformations are performed across basic blocks. ■ Local transformations are only performed on single basic blocks. ■ Transformations must be safe and preserve the meaning of the code . ■ A local transformation is safe if the transformed basic block is guaranteed to be equivalent to its original form
  • 14. Transformations on Basic Blocks Some local transformation are: LocalTransformation Common- Subexpression Elimination Dead Code Elimination Renaming Temporary Variables Interchange of Statements Algebraic Transformations
  • 15. Common-Subexpression Elimination ■ Remove redundant computations ■ 2nd and 4th:compute same expression in fig:1(a) ■ Look at 1st and 3rd in fig:1(b) :b is redefine in 2nd therefore different in3rd, not the same expression fig:1(a) fig:1(b) a := b + c b := a - d c := b + c d := a - d a := b + c b := a - d c := b + c d := b t1 := b * c t2 := a - t1 t3 := b * c t4 := t2 + t3 t1 := b * c t2 := a - t1 t4 := t2 + t1
  • 16. Dead Code Elimination ■ Remove unused statements Assuming a is dead (not used) Remove unreachable code b := a + 1 a := b + c … b := a + 1 … goto L2 b := x + y …
  • 17. Renaming Temporary Variables ■ Temporary variables that are dead at the end of a block can be safely renamed. ■ The basic block is transforms into an equivalent block in which each statement that defines a temporary defines a new temporary. ■ Such a basic block is called normal-form block or simple block. Normal-form block ■ Note that normal-form blocks permit all statement interchanges that are possible. t1 := b + c t2 := a - t1 t1 := t1 * d d := t2 + t1 t1 := b + c t2 := a - t1 t3 := t1 * d d := t2 + t3
  • 18. Interchange of Statements ■ Independent statements can be reordered without effecting the value of block to make its optimal use. ■ Note that normal-form blocks permit all statement interchanges that are possible t1 := b + c t2 := a - t1 t3 := t1 * d d := t2 + t3 t1 := b + c t3 := t1 * d t2 := a - t1 d := t2 + t3
  • 19. Algebraic Transformations ■ Change arithmetic operations to transform blocks to algebraic equivalent forms. ■ Simplify expression or replace expensive expressions by cheaper ones. ■ In statement 3,usually require a function call ■ Transforms to simple and equivalent statement t1 := a - a t2 := b + t1 t3 := t2 **2 t1 := 0 t2 := b t3 := t2 * t2