SlideShare a Scribd company logo
4
Most read
5
Most read
18
Most read
Intermediate Representations
Control Flow Graphs (CFG)
Don by khalid alsediri
COMP2105
Intermediate Representations(IR)
An intermediate representation is a representation of a program
part way between the source and target language
.
IR use many technique for representation
-Structured (graph or tree-based)
-Flat, tuple-based
-Flat, stack-based
-Or any combination of the above three
Optimization
Code transformations to improve program
-minimize execution time.
-reduce program size .
Must be save, the result program should give same
result to all possible input
Control Flow Graphs(CFG)
A control flow graph (CFG) is a data structure for High level
representation or low level representation .
1. break the big problem into smaller piece which are
manageable
2. To perform machine independent optimizations
3. Can easily find unreachable code
4. Makes syntactic structure (like loops) easy to find
The CFG is a directed graph where the vertices represent
basic blocks and edges represent possible transfer of control
flow from one basic block to another
Building CFG
• We divide the intermediate code of each procedure into basic
blocks. A basic block is a piece of straight line code, i.e. there
are no jumps in or out of the middle of a block.
• The basic blocks within one procedure are organized as a
(control) flow graph, or CFG. A flow-graph has
• basic blocks 𝑩 𝟏· · · 𝑩 𝒏 as nodes,
• a directed edge 𝑩 𝟏 𝑩 𝟐 if control can flow from 𝑩 𝟏 to 𝑩 𝟐.
• Special nodes ENTER and EXIT that are the source and sink
of the graph.
• Inside each basic block can be any of the IRs we’ve seen:
tuples, trees, DAGs, etc.
Building CFG
Building the CFG
• High-level representation
– Control flow is implicit in an AST.
• Low-level representation:
– Nodes represent statements (low-level linear IR)
– Edges represent explicit flow of control
• Program
x = z-2 ;
y = 2*z;
if (c) {
x = x+1;
y = y+1;
}
else {
x = x-1;
y = y-1;
}
z = x+y;
x = z-2 ;
y = 2*z;
if (c)
x = x+1;
y = y+1;
x = x-1;
y = y-1;
z = x+y;
B3
B1
B2
B4
FT
Example high level
1 a := 0
2 b := a * b
3 L1: c := b/d
4 if c < x goto L2
5 e := b / c
6 f := e + 1
7 L2: g := f
8 h := t - g
9 if e > 0 goto L3
10 goto L1
11 L3: return
a := 0
b := a * b
c := b/d
if c < x
e := b / c
f := e + 1
g := f
h := t - g
if e > 0
goto return
B1
B2
B3
B4
B6 B5
Low level example
---------Source Code-----------------------
X := 20; WHILE X < 10 DO
X := X-1; A[X] := 10;
IF X = 4 THEN X := X - 2; ENDIF;
ENDDO; Y := X + 5;
---------Intermediate Code---------------
(1) X := 20
(2) if X>=10 goto (8)
(3) X := X-1
(4) A[X] := 10
(5) if X<>4 goto (7)
(6) X := X-2
(7) goto (2)
(8) Y := X+5
X := 20
Y := X+5
goto B2
X := X-2
X := X-1
(4) A[X] := 10
(5) if X<>4 goto B6
if X>=10 goto B4
B1
B2
B4B3
B5
B6
Building basic blocks algorithm
• Identify leaders
1-The first instruction in a procedure, or
2-The target of any branch, or
3-An instruction immediately following a branch (implicit target)
• For each leader, its basic block is the leader
and all statements up to, but not including, the
next leader or the end of the program.
Building basic blocks algorithm
• Input: List of n instructions (instr[i] =𝑖 𝑡ℎ instruction),
A sequence of intermediate code statements
Output: Set of leaders & list of basic blocks
(block[x] is block with leader x)
leaders = {1} // First instruction is a leader
for i = 1 to n // Find all leaders
if instr[i] is a branch
leaders = leaders ∪ set of potential targets of instr[i]
foreach x ∈ leaders //each leader is leader of it self
block[x] = { x }
i = x+1 // Fill out x’s basic block
while i ≤ n and i ∉ leaders
block[x] = block[x] ∪ { i }
i = i + 1
Building basic blocks algorithm
1 a := 0
2 b := a * b
3 L1: c := b/d
4 if c < x got L2
5 e := b / c
6 f := e + 1
7 L2: g := f
8 h := t - g
9 if e > 0 goto L3
10 goto L1
11 L3: return
Building basic blocks algorithm
1 a := 0
2 b := a * b
3 L1: c := b/d
4 if c < x got L2
5 e := b / c
6 f := e + 1
7 L2: g := f
8 h := t - g
9 if e > 0 goto L3
10 goto L1
11 L3: return
Leaders?
– {1, 3, 5, 7, 10, 11}
Blocks?
– {1, 2}
– {3, 4}
– {5, 6}
– {7, 8, 9}
– {10}
– {11}
Building CFG
• Input: A list of m basic blocks (block)
Output: A CFG where each node is a basic block
for i = 1 to m
x = last instruction of block[i]
if instr x is a branch
for each target (to block j) of instr x
create an edge from block i to block j
if instr x is not an unconditional branch
create an edge from block i to block i+1
Building basic blocks algorithm
1 a := 0
2 b := a * b
3 L1: c := b/d
4 if c < x got L2
5 e := b / c
6 f := e + 1
7 L2: g := f
8 h := t - g
9 if e > 0 goto L3
10 goto L1
11 L3: return
Leaders?
– {1, 3, 5, 7, 10, 11}
Blocks?
– {1, 2}
– {3, 4}
– {5, 6}
– {7, 8, 9}
– {10}
– {11}
1 a := 0
2 b := a * b
3 L1: c := b/d
4 if c < x got L2
5 e := b /
c
6 f := e +
1
7 L2: g := f
8 h := t - g
9 if e > 0 goto
L3
10 goto
L1
11 L3:
return
Variation of CFG
• Extended basic blocks
-A maximal sequence of instructions that
-has no merge points in it (except perhaps in the leader)
-Single entry, multiple exits
• Reverse extended basic blocks
-Useful for “backward flow” problems
Reference
• Modern Compilers: Theory , V. Krishna Nandivada,
2015,http://www.cse.iitm.ac.in/~krishna/courses/2015/even-
cs6013/lecture4.pdf ,accessed(19-14-2016).
• Introduction to Compilers,Tim
Teitelbaum,2008,http://www.cs.cornell.edu/courses/cs412/2008sp/l
ectures/lec24.pdf,accessed(19-14-2016).
• Modern Programming Language Implementation , E Christopher
Lewis ,2006,http://www.cis.upenn.edu/~cis570/slides/lecture03.pdf,
accessed(19-14-2016).

More Related Content

What's hot (20)

PPTX
Software project estimation
inayat khan
 
PPT
13. Query Processing in DBMS
koolkampus
 
PPT
Introduction to Software Project Management
Reetesh Gupta
 
PPTX
SRS(software requirement specification)
Akash Kumar Dhameja
 
PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPT
Architecture design in software engineering
Preeti Mishra
 
PPTX
Cohesion and coupling
Aprajita (Abbey) Singh
 
PPTX
Basis path testing
Hoa Le
 
PPT
Black box & white-box testing technique
SivaprasanthRentala1975
 
PPT
Debugging
Indu Sharma Bhardwaj
 
PPT
Software Metrics
swatisinghal
 
PPT
Cocomo model
Bala Ganesh
 
PPTX
Java servlets and CGI
lavanya marichamy
 
PPT
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
PPT
Software Project Management
Ramesh Babu
 
PPTX
Design Concept software engineering
Darshit Metaliya
 
PPTX
Software myths | Software Engineering Notes
Navjyotsinh Jadeja
 
PPT
Formal Specification in Software Engineering SE9
koolkampus
 
PPTX
RMMM Plan
Ankit Bahuguna
 
PPT
Software Engineering (Project Scheduling)
ShudipPal
 
Software project estimation
inayat khan
 
13. Query Processing in DBMS
koolkampus
 
Introduction to Software Project Management
Reetesh Gupta
 
SRS(software requirement specification)
Akash Kumar Dhameja
 
Specification-of-tokens
Dattatray Gandhmal
 
Architecture design in software engineering
Preeti Mishra
 
Cohesion and coupling
Aprajita (Abbey) Singh
 
Basis path testing
Hoa Le
 
Black box & white-box testing technique
SivaprasanthRentala1975
 
Software Metrics
swatisinghal
 
Cocomo model
Bala Ganesh
 
Java servlets and CGI
lavanya marichamy
 
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Software Project Management
Ramesh Babu
 
Design Concept software engineering
Darshit Metaliya
 
Software myths | Software Engineering Notes
Navjyotsinh Jadeja
 
Formal Specification in Software Engineering SE9
koolkampus
 
RMMM Plan
Ankit Bahuguna
 
Software Engineering (Project Scheduling)
ShudipPal
 

Similar to Control Flow Graphs (20)

PDF
Lecture03
sean chen
 
PDF
Control Flow Analysis
Edgar Barbosa
 
PPTX
Bp150520
saraswathi saraswathi
 
PPT
ERTS UNIT 3.ppt
Pavithra525349
 
PDF
Intermediate code generation
Akshaya Arunan
 
PDF
Code optimization in compiler design
Kuppusamy P
 
PPT
unit-5.pptvshvshshhshsjjsjshhshshshhshsj
manasareddyiit
 
PDF
Code optimization lecture
Prashant Singh
 
PPTX
Code Generation Part-2 in Compiler Construction
ProfMonikaShah
 
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
boyingbo
 
PPT
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
PPTX
Basic blocks and control flow graphs
Tilakpoudel2
 
PPT
458237.-Compiler-Design-Intermediate-code-generation.ppt
PalaniSamyB3
 
PPTX
Building blocks of Algblocks of Alg.pptx
NISHASOMSCS113
 
PDF
Compiler unit 4
BBDITM LUCKNOW
 
PPTX
UNIT 1.pptx Programming for Problem Solving
ramesh130484
 
PPT
Compiler Design Unit 5
Jena Catherine Bel D
 
PDF
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
Lecture03
sean chen
 
Control Flow Analysis
Edgar Barbosa
 
ERTS UNIT 3.ppt
Pavithra525349
 
Intermediate code generation
Akshaya Arunan
 
Code optimization in compiler design
Kuppusamy P
 
unit-5.pptvshvshshhshsjjsjshhshshshhshsj
manasareddyiit
 
Code optimization lecture
Prashant Singh
 
Code Generation Part-2 in Compiler Construction
ProfMonikaShah
 
456589.-Compiler-Design-Code-Generation (1).ppt
boyingbo
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
Basic blocks and control flow graphs
Tilakpoudel2
 
458237.-Compiler-Design-Intermediate-code-generation.ppt
PalaniSamyB3
 
Building blocks of Algblocks of Alg.pptx
NISHASOMSCS113
 
Compiler unit 4
BBDITM LUCKNOW
 
UNIT 1.pptx Programming for Problem Solving
ramesh130484
 
Compiler Design Unit 5
Jena Catherine Bel D
 
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
Ad

Recently uploaded (20)

PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
community health nursing question paper 2.pdf
Prince kumar
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
Ad

Control Flow Graphs

  • 1. Intermediate Representations Control Flow Graphs (CFG) Don by khalid alsediri COMP2105
  • 2. Intermediate Representations(IR) An intermediate representation is a representation of a program part way between the source and target language . IR use many technique for representation -Structured (graph or tree-based) -Flat, tuple-based -Flat, stack-based -Or any combination of the above three
  • 3. Optimization Code transformations to improve program -minimize execution time. -reduce program size . Must be save, the result program should give same result to all possible input
  • 4. Control Flow Graphs(CFG) A control flow graph (CFG) is a data structure for High level representation or low level representation . 1. break the big problem into smaller piece which are manageable 2. To perform machine independent optimizations 3. Can easily find unreachable code 4. Makes syntactic structure (like loops) easy to find The CFG is a directed graph where the vertices represent basic blocks and edges represent possible transfer of control flow from one basic block to another
  • 5. Building CFG • We divide the intermediate code of each procedure into basic blocks. A basic block is a piece of straight line code, i.e. there are no jumps in or out of the middle of a block. • The basic blocks within one procedure are organized as a (control) flow graph, or CFG. A flow-graph has • basic blocks 𝑩 𝟏· · · 𝑩 𝒏 as nodes, • a directed edge 𝑩 𝟏 𝑩 𝟐 if control can flow from 𝑩 𝟏 to 𝑩 𝟐. • Special nodes ENTER and EXIT that are the source and sink of the graph. • Inside each basic block can be any of the IRs we’ve seen: tuples, trees, DAGs, etc.
  • 7. Building the CFG • High-level representation – Control flow is implicit in an AST. • Low-level representation: – Nodes represent statements (low-level linear IR) – Edges represent explicit flow of control
  • 8. • Program x = z-2 ; y = 2*z; if (c) { x = x+1; y = y+1; } else { x = x-1; y = y-1; } z = x+y; x = z-2 ; y = 2*z; if (c) x = x+1; y = y+1; x = x-1; y = y-1; z = x+y; B3 B1 B2 B4 FT Example high level
  • 9. 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x goto L2 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 10 goto L1 11 L3: return a := 0 b := a * b c := b/d if c < x e := b / c f := e + 1 g := f h := t - g if e > 0 goto return B1 B2 B3 B4 B6 B5 Low level example
  • 10. ---------Source Code----------------------- X := 20; WHILE X < 10 DO X := X-1; A[X] := 10; IF X = 4 THEN X := X - 2; ENDIF; ENDDO; Y := X + 5; ---------Intermediate Code--------------- (1) X := 20 (2) if X>=10 goto (8) (3) X := X-1 (4) A[X] := 10 (5) if X<>4 goto (7) (6) X := X-2 (7) goto (2) (8) Y := X+5 X := 20 Y := X+5 goto B2 X := X-2 X := X-1 (4) A[X] := 10 (5) if X<>4 goto B6 if X>=10 goto B4 B1 B2 B4B3 B5 B6
  • 11. Building basic blocks algorithm • Identify leaders 1-The first instruction in a procedure, or 2-The target of any branch, or 3-An instruction immediately following a branch (implicit target) • For each leader, its basic block is the leader and all statements up to, but not including, the next leader or the end of the program.
  • 12. Building basic blocks algorithm • Input: List of n instructions (instr[i] =𝑖 𝑡ℎ instruction), A sequence of intermediate code statements Output: Set of leaders & list of basic blocks (block[x] is block with leader x) leaders = {1} // First instruction is a leader for i = 1 to n // Find all leaders if instr[i] is a branch leaders = leaders ∪ set of potential targets of instr[i] foreach x ∈ leaders //each leader is leader of it self block[x] = { x } i = x+1 // Fill out x’s basic block while i ≤ n and i ∉ leaders block[x] = block[x] ∪ { i } i = i + 1
  • 13. Building basic blocks algorithm 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x got L2 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 10 goto L1 11 L3: return
  • 14. Building basic blocks algorithm 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x got L2 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 10 goto L1 11 L3: return Leaders? – {1, 3, 5, 7, 10, 11} Blocks? – {1, 2} – {3, 4} – {5, 6} – {7, 8, 9} – {10} – {11}
  • 15. Building CFG • Input: A list of m basic blocks (block) Output: A CFG where each node is a basic block for i = 1 to m x = last instruction of block[i] if instr x is a branch for each target (to block j) of instr x create an edge from block i to block j if instr x is not an unconditional branch create an edge from block i to block i+1
  • 16. Building basic blocks algorithm 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x got L2 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 10 goto L1 11 L3: return Leaders? – {1, 3, 5, 7, 10, 11} Blocks? – {1, 2} – {3, 4} – {5, 6} – {7, 8, 9} – {10} – {11} 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x got L2 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 10 goto L1 11 L3: return
  • 17. Variation of CFG • Extended basic blocks -A maximal sequence of instructions that -has no merge points in it (except perhaps in the leader) -Single entry, multiple exits • Reverse extended basic blocks -Useful for “backward flow” problems
  • 18. Reference • Modern Compilers: Theory , V. Krishna Nandivada, 2015,http://www.cse.iitm.ac.in/~krishna/courses/2015/even- cs6013/lecture4.pdf ,accessed(19-14-2016). • Introduction to Compilers,Tim Teitelbaum,2008,http://www.cs.cornell.edu/courses/cs412/2008sp/l ectures/lec24.pdf,accessed(19-14-2016). • Modern Programming Language Implementation , E Christopher Lewis ,2006,http://www.cis.upenn.edu/~cis570/slides/lecture03.pdf, accessed(19-14-2016).