G27
Self-checkedTier 1 9 min readcd.basic-blocks-cfg

Basic blocks and control flow graphs

Dividing three-address code into basic blocks by finding leaders, the control flow graph with its edges, entry and exit, and the loop vocabulary (dominators, back edges, natural loops), with counting questions on blocks and edges.

Why it exists

Most optimisation and code generation work on straight-line code: sequences of instructions that are always executed from the first to the last. A long program is first cut into such pieces, the basic blocks, and the possible transfers of control between them are drawn as a graph, the control flow graph (CFG). Local optimisation works inside one block; global optimisation and data flow analysis work on the whole graph (data flow framework).

Prerequisites

You need three-address code with jumps (three-address code), as produced by control-flow translation.

Precise definitions

Basic block. A maximal sequence of consecutive three-address instructions such that (1) control can enter only at the first instruction (there is no jump into the middle), and (2) control leaves only at the last instruction, without halting or branching in the middle, except possibly at the last. If the first instruction of a block is executed, all the others are executed in order.

Leaders. The first instruction of each basic block. They are found by three rules:

  1. The first instruction of the program is a leader.
  2. Any instruction that is the target of a conditional or unconditional jump is a leader.
  3. Any instruction that immediately follows a conditional or unconditional jump is a leader.

Each leader starts a block that extends up to, but not including, the next leader (or to the end of the program). A leader found by two rules is a leader once, so the number of blocks equals the number of distinct leaders.

Control flow graph. The nodes are the basic blocks. There is a directed edge from block B to block C if C can immediately follow B in some execution, that is:

  • the last instruction of B is a conditional or unconditional jump to the first instruction of C; or
  • C immediately follows B in the program text and B does not end in an unconditional jump (goto).

A block that ends in a conditional jump has two successors (the target and the next block); a block that ends in goto has one; a block that ends in neither has one (the next block). Textbooks often add an entry node (with an edge to the first block) and an exit node (reached from blocks that end the program); the count of edges in a question may or may not include them, so read the statement.

Loops. A node dd dominates a node nn if every path from the entry to nn passes through dd (every node dominates itself). An edge n→hn \to h is a back edge if hh dominates nn. The natural loop of a back edge n→hn \to h is hh together with all nodes that can reach nn without passing through hh; hh is the header. A loop is where the program spends most of its time, so optimisations such as loop-invariant code motion concentrate on loops.

How it works

The animation finds the leaders of a program with two nested loops (a typical double loop that fills a matrix) rule by rule, then the blocks, then the edges. Watch which rule makes each instruction a leader: instruction 2 is the target of the jump in instruction 11 (the end of the outer loop), instruction 3 the target of the jump in 9 (the inner loop), 10 and 12 follow jumps, and 13 is the target of the jump in 17. Instruction 1 is a leader by rule 1. That gives six blocks and eight edges. The inner loop is the block B3 with an edge to itself, and the outer loop is B2, B3, B4.

Leaders, basic blocks and flow graph of two nested loops: start

Computed by a script, not typed
#instructionleader becauseblockedges out of the block
1i = 1
2j = 1
3t1 = 10 * i
4t2 = t1 + j
5t3 = 8 * t2
6t4 = t3 - 88
7a[t4] = 0
8j = j + 1
9if j <= 10 goto 3
10i = i + 1
11if i <= 10 goto 2
12i = 1
13t5 = i - 1
14t6 = 88 * t5
15a[t6] = 1
16i = i + 1
17if i <= 10 goto 13

Step 0

A basic block is a maximal sequence of consecutive instructions with one entry (the first instruction) and one exit (the last). To find the blocks, first find the leaders, the first instructions of blocks: (1) the first instruction of the code; (2) the target of every jump; (3) the instruction that comes right after a jump.

0 / 8
computed by script: the algorithm is executed, one step at a time. python3 scripts/gen-cd-icg-traces.py

Worked examples

EX 1Find the leaders

Easy

In the animation's program the leaders are instructions 1, 2, 3, 10, 12 and 13. Blocks: B1 = {1}, B2 = {2}, B3 = {3, ..., 9}, B4 = {10, 11}, B5 = {12}, B6 = {13, ..., 17}. Note that instruction 18 does not exist, so the jump in 17 has only the target 13 as an extra leader and no instruction follows to become a leader. Edges: B1 → B2, B2 → B3, B3 → B3 and B3 → B4 (the jump in 9), B4 → B2 and B4 → B5 (the jump in 11), B5 → B6, B6 → B6 (the jump in 17): 8 edges.

EX 2A while loop

Easy

while (i < 10) { s = s + i; i = i + 1; } translates (textbook style) to: 1: ifFalse i < 10 goto 7, 2: t1 = s + i, 3: s = t1, 4: t2 = i + 1, 5: i = t2, 6: goto 1. Leaders: 1 (first, and target of the jump in 6), 2 (follows the jump in 1); instruction 7 would be the target of the jump in 1 but it is past the end of the code. Blocks: B1 = {1} (the test) and B2 = {2, ..., 6} (the body). Edges: B1 → B2 (fall through when the test succeeds), B2 → B1 (the goto), and the exit from B1 when the test fails: 2 blocks, 2 edges (plus the exit edge if an exit node is drawn).

EX 3if-else

Medium

if (a < b) x = 1; else x = 2; y = x; gives 1: ifFalse a < b goto 4, 2: x = 1, 3: goto 5, 4: x = 2, 5: y = x. Leaders: 1, 2 (after the jump in 1), 4 (target of the jump in 1, and follows the goto in 3), 5 (target of the goto in 3). Blocks: B1 = {1}, B2 = {2, 3}, B3 = {4}, B4 = {5}. Edges: B1 → B2, B1 → B3, B2 → B4 (the goto), B3 → B4: 4 blocks, 4 edges; B2 has no edge to B3 because it ends in an unconditional jump.

EX 4Loops of the nested-loop graph

Medium

Dominators: B1 dominates everything; B2 dominates B3, B4, B5, B6; B3 dominates B4; B4 dominates B5; B5 dominates B6. Back edges (head dominates tail): B3 → B3, B4 → B2, B6 → B6. Natural loops: for B3 → B3 the loop is {B3}; for B4 → B2 it is {B2, B3, B4} (all nodes that reach B4 without passing B2: B3 and B4); for B6 → B6 it is {B6}. So there are three loops, one nested inside another. The edge B2 → B3 is not a back edge because B3 does not dominate B2.

EX 5Count blocks from a listing

GATE level

A 3AC listing: 1: a = 1, 2: b = 2, 3: if a > b goto 6, 4: c = a + b, 5: goto 7, 6: c = a - b, 7: print c. Leaders by the rules: 1 (rule 1); 6 and 7 (targets); 4 (follows the jump in 3) and 6 (follows the goto in 5, also a target: counts once). Leaders: {1, 4, 6, 7}: 4 blocks: B1 = {1, 2, 3}, B2 = {4, 5}, B3 = {6}, B4 = {7}. Edges: B1 → B2, B1 → B3, B2 → B4, B3 → B4: 4 edges. Typical mistakes: counting 6 twice, or forgetting the leader after a jump.

Cheat box

Cheat box

  • Leaders: first instruction; target of any jump; instruction after any jump. Blocks run from a leader to the next leader.
  • Number of blocks = number of distinct leaders.
  • Edge B to C: B jumps to C's first instruction, or C follows B and B does not end in an unconditional goto.
  • Conditional jump: two successors; goto: one; other: next block.
  • Dominator: on every path from the entry. Back edge: head dominates tail. Natural loop: header plus nodes that reach the tail without passing the header.
  • Entry and exit nodes may or may not be counted.

Traps and GATE patterns

Connections

The jump instructions and labels come from control-flow translation; the instructions inside a block are rewritten by local optimisation and described by a DAG; the graph is the framework of data flow analysis. The notion of a basic block also underlies pipelining and branch hazards at the hardware level.

PYQs

5 previous year questions from your book. I re-solved each one myself (4 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2017 · NATQ20 · page 259 of the book
Consider the following grammar:
            stmt    → if expr then expr else expr; stmt|O`
            expr    →  term relop term | term
            term    →  id | number
             id        →  a | b |  c
           number  → [0-9]

where relop is a relational operator (e.g., <, >, ...),
O ` refers to the empty statement, and if, then, else
are terminals.

Consider a program P following the above
grammar containing ten if terminals. The number
of control flow paths in P is ________. For
example, the program

 if e1 then e2 else e3

has 2 control flow paths, e1  → e2 and e1  → e3
GATE 2025 · NATQ4 · page 280 of the book
Refer to the given 3-address code sequence. This
code sequence is split into basic blocks. The
number of basic blocks is _________.

1001: i = 1

1002: j = 1

1003: t1 = 10*i

1004: t2 = t1+j

1005: t3 = 8*t2

1006: t4 = t3-88

1007: a[t4] = 0.0

1008: j = j+1

1009: if j <= 10 goto 1003

1010: i = i+1

1011: if i <= 10 goto 1002

1012: i = 1

1013: t5 = i-1

1014: t6 = 88*t5

1015: a[t6] = 1.0

1016: i = i+1

1017: if i <= 10 goto 1013
GATE 2024 · MCQQ7 · page 280 of the book
Consider the following pseudo-code.

L1 : t1 = -1

L2 : t2 = 0

L3 : t3 = 0

L4 : t4 = 4 * t3

L5 : t5 = 4 * t2

L6 : t6 = t5 * M

L7 : t7 = t4 + t6

L8 : t8 = a[t7]

L9 : if t8 <= max goto L11

L10 : t1 = t8

L11 : t3 = t3 + 1

L12 : if t3 < M goto L4

L13 : t2 = t2 + 1

L14 : if t2 < N goto L3

L15 : max = t1
Which of the following options CORRECTLY
specifies the number of basic blocks and the number
of
instructions
in
the
largest
basic
block,
respectively?

COMPILER DESIGN
  • (a)6 and 6
  • (b)6 and 7
  • (c)7 and 7
  • (d)7 and 6
GATE 2015 · MCQQ13 · page 281 of the book
In the context of abstract-syntax-tree (AST) and
control-flow-graph (CFG), which one of the
following is TRUE?

COMPILER DESIGN
  • (a)In both AST and CFG, let node N2 be the successor of node N1. In the input program, the code corresponding to N2 is present after the code corresponding in N1.
  • (b)For any input program, neither AST nor CFG will contain a cycle
  • (c)The maximum number of successors of a node in an AST and a CFG depends on the input program
  • (d)Each node is AST and CFG corresponds to at most one statement in the input program.
GATE 2015 · MCQQ14 · page 282 of the book
Consider the intermediate code given below.

(1) i = 1

(2) j = 1

(3) t1 = 5 * i

(4) t2 = t1 + j

(5) t3 = 4 * t2

(6) t4 = t3

(7) a[t4] = –1

(8) j = j + 1

(9) if j < = 5 goto (3)

(10) i = i + 1

(11) if i < 5 goto (2)

The number of nodes and edges in the control-flow-
graph constructed for the above code, respectively,
are:-
  • (a)5 and 7
  • (b)6 and 7
  • (c)5 and 5
  • (d)7 and 8

Self-check

Question 1

A program has 20 instructions. Instructions 5 and 12 are conditional jumps to 3 and 18 respectively. Which are the leaders and how many blocks are there?

Show answer

Leaders: 1, 3, 18 (targets and first), 6 and 13 (after the jumps): {1, 3, 6, 13, 18}: 5 basic blocks.

Question 2

A block ends in goto L. How many successors does it have in the CFG?

Show answer

One: the block that starts at L. There is no fall-through edge.

Question 3

What is a back edge?

Show answer

An edge n→hn \to h in the CFG where hh dominates nn; it closes a natural loop with header hh.

Question 4

Why is the basic block the natural unit for local optimisation?

Show answer

Inside a block control flows straight from the first to the last instruction, so the optimiser can reason about the values without considering jumps.

Revision card

One-screen revision

  • Leaders: first instruction, jump targets, instructions after jumps; blocks = distinct leaders.
  • CFG edges: jump to target, fall-through unless the block ends in goto.
  • Dominators, back edge n→hn \to h with hh dom nn, natural loops.
  • Counting: mark leaders, split, add edges.

References

  • ·Aho, Lam, Sethi, Ullman, Compilers: Principles, Techniques, and Tools (2nd edition), Chapter 8 Code Generation (8.4 basic blocks and flow graphs; 8.4.5 loops) and Chapter 9 (9.6.1 dominators, natural loops)

Verification

Leaders, blocks and edges are computed by script (scripts/cd_icg.py) and rechecked in scripts/enum-checks/cd_icg.py against the behaviour of random executions: on 800 random programs control enters a block only at a leader, runs straight through it, and every observed transition is an edge of the flow graph. The nested-loop example (6 blocks, 8 edges; back edges B3 to B3, B4 to B2, B6 to B6) was recomputed with an iterative dominator algorithm. A jump to the instruction after the last one is treated as leaving the graph (no edge); books differ on whether they add an explicit exit node. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (token counts, FIRST and FOLLOW sets, LL(1) tables and moves, LR item sets, state counts and parses, operator-precedence values, attribute evaluation and printing schemes, activation-record counts and access links, parameter-passing results, three-address code, jumping and backpatched code, leaders and blocks, value numbering and DAGs, gen/kill and data flow sets, Sethi-Ullman labels and live ranges); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never