
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 .
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.
A Basic Block is a sequence of consecutive statements in which:
π In simple words:
A straight-line code segment with no jumps in or out (except at entry and exit).
β Single entry point β Single exit point β No jump in middle β No jump out except last statement
A leader is the first statement of a basic block.
A statement is a leader if:
Each leader starts a new basic block until next leader appears.
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
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
A Flow Graph (or Control Flow Graph) is a directed graph where:
β Represents program structure β Helps in optimization β Used in dead code elimination β Helps in loop detection β Used in register allocation
From previous basic blocks:
B1
/ \
v v
B2 B3
\ /
v v
B4
Example:
goto L2
Example:
if a > b goto L1
β Directed graph β Nodes = basic blocks β Edges = control flow β Has entry and exit nodes β May contain loops
A loop is a cycle in a flow graph where control returns to a previous block.
B2 β B3 β B4 β B2 (cycle)
Basic blocks and flow graphs are used for:
β Dead code elimination β Common subexpression elimination β Loop optimization β Code motion β Register allocation
β 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
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.
| 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 |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.