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 load past papers