ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Basic Blocks Flow Grap…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 31 of 32
    Topic Study Note Illustration
    31

    Basic Blocks and Flow Graphs

    These topics belong to the Code Optimization phase of a compiler. They help in analyzing program structure so that the compiler can optimize execution efficiently .

    🧠 Basic Blocks and Flow Graphs (Compiler Construction)

    These topics belong to the Code Optimization phase of a compiler. They help in analyzing program structure so that the compiler can optimize execution efficiently.


    πŸ“Œ 1. Basic Block

    πŸ“– Definition

    A Basic Block is a sequence of consecutive statements in which:

    • Control enters only at the beginning
    • Control leaves only at the end
    • No branching occurs inside the block

    πŸ‘‰ In simple words:

    A straight-line code segment with no jumps in or out (except at entry and exit).


    βš™οΈ 2. Characteristics of Basic Block

    βœ” Single entry point βœ” Single exit point βœ” No jump in middle βœ” No jump out except last statement


    🧠 3. How to Identify Basic Blocks (VERY IMPORTANT)

    Step 1: Find Leaders

    A leader is the first statement of a basic block.

    A statement is a leader if:

    1. It is the first statement of program
    2. It is the target of a jump (label)
    3. It immediately follows a jump instruction

    Step 2: Form Blocks

    Each leader starts a new basic block until next leader appears.


    πŸ“Š Example

    Code:

    1. a = b + c
    2. d = a + e
    3. if a > b goto L1
    4. x = y + z
    5. goto L2
    6. L1: p = q + r
    7. L2: s = t + u
    

    πŸ”Ή Step 1: Identify Leaders

    • 1 (first statement)
    • 4 (after conditional jump)
    • 6 (label L1)
    • 7 (label L2)

    πŸ”Ή Step 2: Form Basic Blocks

    πŸ”Ή Block B1:

    1. a = b + c
    2. d = a + e
    3. if a > b goto L1
    

    πŸ”Ή Block B2:

    4. x = y + z
    5. goto L2
    

    πŸ”Ή Block B3:

    6. L1: p = q + r
    

    πŸ”Ή Block B4:

    7. L2: s = t + u
    

    πŸ“Œ 4. Flow Graph (Control Flow Graph - CFG)

    πŸ“– Definition

    A Flow Graph (or Control Flow Graph) is a directed graph where:

    • Nodes represent basic blocks
    • Edges represent flow of control

    🧠 5. Purpose of Flow Graph

    βœ” Represents program structure βœ” Helps in optimization βœ” Used in dead code elimination βœ” Helps in loop detection βœ” Used in register allocation


    πŸ“Š Flow Graph Example

    From previous basic blocks:

       B1
      /  \
     v    v
    B2    B3
      \    /
       v  v
        B4
    

    πŸ”Ή Explanation:

    • B1 β†’ B2 (false condition)
    • B1 β†’ B3 (true condition)
    • B2 β†’ B4 (jump)
    • B3 β†’ B4 (fall-through or jump)

    🧠 6. Types of Edges in Flow Graph

    πŸ”Ή 1. Unconditional Edge

    • Always executed

    Example:

    goto L2
    

    πŸ”Ή 2. Conditional Edge

    • Based on condition

    Example:

    if a > b goto L1
    

    πŸ”Ή 3. Fall-through Edge

    • Next sequential block

    🧠 7. Properties of Flow Graph

    βœ” Directed graph βœ” Nodes = basic blocks βœ” Edges = control flow βœ” Has entry and exit nodes βœ” May contain loops


    🧠 8. Loops in Flow Graph

    πŸ“– Definition

    A loop is a cycle in a flow graph where control returns to a previous block.


    πŸ”Ή Example:

    B2 β†’ B3 β†’ B4 β†’ B2 (cycle)
    

    ⭐ Loop Types:

    • Natural loop (single entry)
    • Irreducible loop (multiple entries)

    🧠 9. Importance in Compiler Optimization

    Basic blocks and flow graphs are used for:

    βœ” Dead code elimination βœ” Common subexpression elimination βœ” Loop optimization βœ” Code motion βœ” Register allocation


    πŸ“Œ 10. Key Exam Points

    βœ” Basic block = straight-line code sequence βœ” Entry only at first statement βœ” Flow graph = graph of basic blocks βœ” Nodes = basic blocks βœ” Edges = control flow βœ” Used for optimization βœ” Helps identify loops


    🎯 Final Exam Definition

    A Basic Block is a sequence of consecutive statements in which control enters only at the beginning and leaves only at the end without any jumps in between. A Flow Graph (Control Flow Graph) is a directed graph where nodes represent basic blocks and edges represent the flow of control between them, used for program optimization.


    πŸ“Š FINAL REVISION TABLE

    Concept Meaning Key Feature
    Basic Block Straight-line code Single entry/exit
    Leader First statement of block Starts block
    Flow Graph Control structure graph Nodes + edges
    Node Basic block Code segment
    Edge Control transfer Jump/flow
    Loop Cycle in graph Repetition

    ← Previous topic 30
    Target Language and Addresses in Target Code
    Next topic 32 β†’
    Optimization of Basic Blocks

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    696words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction