Compiler Design · Basic Blocks and Control Flow Graphs
Identifying Leaders to Find Basic Blocks
Overview
Basic blocks are the units of code used for optimization, and they are found by first locating leaders. Identifying leaders is the entry point of the chain Optimization → Loops → Program Flow Graph → Basic Blocks → Leaders, so the whole analysis reduces to finding the leaders.
Key concepts
- Leader: a special statement in the program that marks the start of a basic block.
- Basic block: the code starting at one leader and running up to, but not including, the next leader. If line 1 is a leader and line 10 is the next leader, lines 1–9 form one basic block.
- Motivation chain: to apply optimization we must find the loops; to find the loops we must find the program flow graph (PFG); to find the PFG we must find the basic blocks; to find the basic blocks we must find the leaders.
How it works
- Find all the leaders in the program.
- A basic block begins at a leader and continues until the next leader, excluding that next leader.
Rules for identifying leaders:
- The first statement of the program is always a leader. In a program of 100 lines, line number 1 is always a leader.
- A statement that is the target of a conditional or unconditional jump is a leader. For
if ... goto 100, line 100 is a leader; forgoto 200, line 200 is a leader.
Whether the jump is conditional or unconditional, the target of the goto is always a leader. - The statement that immediately follows a conditional or unconditional jump is a leader. If a goto statement is at line number 10, then line number 11 is a leader.
Drawing the diagram…
Worked examples
The procedure is applied to an actual program in three stages: first find the leaders, then from the leaders find the basic blocks, then perform control flow analysis to find the loops. (The full worked example on the factorial program follows in the next part.)
Revision summary
- A basic block excludes the next leader: leader at line 1, next leader at line 10 → block is lines 1–9.
- The first statement of the program is always a leader.
- Both conditional and unconditional jumps make their target a leader.
- The statement immediately after a conditional or unconditional jump is also a leader.
- Memorize the chain: Optimization → Loops → Program Flow Graph → Basic Blocks → Leaders.
Previous-year exam insights
- A basic block has exactly one entry point (its first statement) and exactly one exit point (its last statement); control can enter only at the beginning and leave only at the end.
Asked inGATE 2025 - The number of basic blocks equals the number of leaders; every leader starts a new basic block.
Asked inGATE 2025 - A conditional jump statement is itself a leader if it is the target of another jump or immediately follows a jump; otherwise it is not a leader merely because it is a jump.
- A procedure call does not by itself create a new basic block; only the three leader rules determine block boundaries.
- The last statement of a basic block is either a jump (conditional or unconditional) or the statement immediately before a leader; otherwise it is not a block boundary.
- In a control flow graph, nodes are basic blocks and edges represent possible flow of control between them; a block with multiple successors corresponds to a conditional jump.