SlideShare a Scribd company logo
1 of 21
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

Type checking
Type checkingType checking
Type checking
rawan_z
 

What's hot (20)

Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Basic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler ConstructionBasic blocks and flow graph in Compiler Construction
Basic blocks and flow graph in Compiler Construction
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Three Address code
Three Address code Three Address code
Three Address code
 
Loops in flow
Loops in flowLoops in flow
Loops in flow
 
Spell checker using Natural language processing
Spell checker using Natural language processing Spell checker using Natural language processing
Spell checker using Natural language processing
 
Type checking
Type checkingType checking
Type checking
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 

Similar to Basic blocks and control flow graphs

Similar to Basic blocks and control flow graphs (20)

Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
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
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
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
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
 

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

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

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