Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Basic blocks and flow graphs

How to split a program's instruction stream into basic blocks and build a control-flow graph (CFG) — definitions, exact leader algorithm, how to draw and use CFGs in compiler optimisations and analyses.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Basic blocks and control-flow graphs are the fundamental data structures the back end of a compiler uses for optimisations and analyses (for example, liveness analysis, register allocation, and code motion). An engineer uses basic blocks and CFGs when implementing optimisation passes, generating machine code, or reasoning about program control structure in tools such as static analysers and profilers.

Key ideas

Start from the instruction stream of an intermediate representation (commonly three-address code (TAC)). We need a simple unit that guarantees no internal control flow so analyses can treat the unit as atomic.

Definition: Basic block — A maximal sequence of consecutive instructions with these two properties:

  • Single-entry: control enters the sequence only at the first instruction.
  • Single-exit: control leaves the sequence only at the last instruction (except by an abnormal event like exception which we do not model here).

Consequences (exact, exam-style statements):

  • No instruction inside a basic block (other than the first) can be the target of a jump or branch instruction.
  • Only the last instruction of a basic block may be a jump or branch; other instructions must execute sequentially.

Definition: Leader — An instruction that begins a basic block. The standard algorithm identifies leaders with three rules (exact algorithm below).

Algorithm (Leader algorithm) — exact statement used in university answers:

  1. The first instruction of the function is a leader.
  2. Any instruction that is the target of a conditional or unconditional jump (a label) is a leader.
  3. Any instruction that immediately follows a conditional or unconditional jump instruction is a leader (to begin the fall-through block for a conditional and to start a new block after an unconditional transfer so other jumps can target it).

Construction of basic blocks from leaders (exact step-by-step):

  • After marking leaders, form a basic block for each leader consisting of the leader and all subsequent instructions up to (but not including) the next leader or until the function ends. Each leader begins exactly one basic block.

Definition: Control-flow graph (CFG) — A directed graph G = (N, E) where:

  • Each node n ∈ N corresponds to a basic block.
  • There is a directed edge (A → B) ∈ E if control can transfer from the last instruction of block A to the first instruction of block B at runtime.

How to decide edges (exact rules):

  • If the last instruction of block A is an unconditional goto L, then there is an edge to the block whose first instruction is L.
  • If the last instruction of A is a conditional if cond goto L, then there are two edges: one to the block starting at label L (the taken branch) and one to the fall-through block (the block that starts at the next sequential instruction after A) if that instruction exists.
  • If the last instruction is a return/exit, it has no outgoing edges (it is a sink in the CFG).
  • If the last instruction of A is not an explicit control-transfer (neither goto/if-goto/return), then there is a fall-through edge to the next basic block in program order (the one that starts at the next leader), if it exists.

Definition: Entry block — the block that contains the first instruction of the function. Definition: Exit block(s) — blocks that can terminate execution (return) and thus have no successors. Definition: Successor — for block A, any block B such that edge A→B exists. Definition: Predecessor — for block B, any block A with A→B.

Why this works: the leader rules guarantee maximal sequences with single entry and exit. Proof sketch commonly expected in exams: show that every non-leader instruction cannot be reached from outside its block (because it is not a jump target and not the first instruction), and there is no exit from the block except at its last instruction (because any internal jump would have made the following instruction a leader).

What to draw (diagram instructions for exams):

  • Make one rectangle (box) per basic block. Inside each box write the block name (e.g., B1) and the instructions in sequence (line numbers if given).
  • Draw directed arrows from a block to its successor blocks. Label edges if helpful (e.g., true/false for conditional branches).
  • Mark the entry block with an incoming arrow labelled ENTRY and mark exit blocks with a small circle or label EXIT.

Where basic blocks and CFGs are used in the compiler course: data-flow analysis, machine-independent optimisations, register allocation, DAG construction per basic block, and instruction scheduling.

Formulas

label: — defines a label (target) in TAC; the label is the start of a potential leader. x = y op z — typical three-address code arithmetic/assignment; appears inside a basic block (no control change). goto L — unconditional jump; creates an edge from current block to the block starting at label L. if cond goto L — conditional jump; creates two edges: to L (taken) and to the fall-through block (not-taken). return x — function exit; block with this as last instruction has no successors.

For CFG representation in code/notes, remember: succ(B) — set of successor blocks of B; pred(B) — set of predecessor blocks of B.

Worked examples

Example 1 (simple sequence and conditional). Given three-address code (with line numbers): 1: a = b + c 2: if a < 10 goto L1 3: d = a * 2 4: goto L2 5: L1: d = a - 1 6: L2: return d

Step 1 — find leaders by the three rules:

  • Instruction 1 is a leader (rule 1).
  • Targets of jumps: L1 at line 5 is a leader (rule 2). L2 at line 6 is a leader (rule 2).
  • Instructions immediately following jumps: instruction 3 follows the conditional at line 2, so instruction 3 is a leader if line 2 is a jump (rule 3). But line 3 is already between leaders 1 and 5 — check carefully: since the conditional at line 2 may fall through to 3, instruction 3 must be a leader. (So leaders: 1, 3, 5, 6.)

Step 2 — form blocks (group until next leader):

  • B1: lines 1-2 (leader 1 through before leader 3).
  • B2: line 3-4 (leader 3 through before leader 5).
  • B3: line 5 (leader 5 only).
  • B4: line 6 (leader 6 only).

Step 3 — determine edges using last instruction of each block:

  • B1 ends with if a < 10 goto L1: edges B1 → B3 (taken) and B1 → B2 (fall-through).
  • B2 ends with goto L2: edge B2 → B4.
  • B3 ends with fall-through to B4 (line 5 followed by line 6): edge B3 → B4.
  • B4 ends with return: no successors.

Succ/Pred sets for this example:

  • succ(B1) = {B3, B2}; succ(B2) = {B4}; succ(B3) = {B4}; succ(B4) = {}
  • pred(B1) = {}; pred(B2) = {B1}; pred(B3) = {B1}; pred(B4) = {B2, B3}

Diagram to draw: four boxes B1..B4 with arrows B1→B3 (label "true"), B1→B2 (label "false"), B2→B4, B3→B4. Mark B1 as ENTRY and B4 as EXIT.

Example 2 (loop example). Given TAC: 1: i = 0 2: L: if i >= n goto Lend 3: sum = sum + A[i] 4: i = i + 1 5: goto L 6: Lend: return sum

Leaders: 1 (first instr), 2 (label L), 6 (label Lend). Instruction after conditional (3) is also a leader by rule 3. So leaders: 1, 2, 3, 6. Blocks:

  • B1: 1 (initialisation)
  • B2: 2 (the conditional)
  • B3: 3-5 (the loop body; instructions 3,4,5) — note 5 is a goto L so B3 ends with a jump
  • B4: 6 (return) Edges:
  • B1 fall-through → B2
  • B2 true (i>=n) → B4; B2 false → B3
  • B3 goto L → B2
  • B4 has no successors

Diagram: boxes B1..B4, arrows B1→B2, B2→B4 (true), B2→B3 (false), B3→B2 (back edge). Label the back edge in the diagram as a loop edge.

These two worked examples show leader identification, block construction, and edge creation.

Common mistakes

  • Incorrect grouping: stopping a block at a conditional instead of including the conditional as the last instruction of the block.
  • Treating every label as a separate block even when labels are consecutive (consecutive labels can start the same block if no instruction between them is a leader by other rules).
  • Drawing edges only for taken branches and forgetting fall-through edges.
  • Confusing instruction numbers and block numbers in diagrams; always list instructions inside boxes and use block names on arrows.

For your exam

Typical question types:

  • Define basic block and CFG (short note): give exact definitions and properties.
  • Given TAC with line numbers: identify leaders, list basic blocks, draw CFG, give succ()/pred() sets.
  • Prove or explain why the leader algorithm produces maximal single-entry single-exit blocks (derivation/proof sketch required).
  • Show how CFG is used in a later topic (data-flow analysis, register allocation) — explain briefly. What a full-marks answer includes:
  • Precise definitions with bold labels if required.
  • Step-by-step application of the three leader rules showing which lines become leaders and why.
  • Clear listing of each basic block with its instruction range.
  • A neat diagram: boxes labelled B1..Bk, arrows for edges, entry/exit marked, and edge labels for conditional branches.
  • Successor and predecessor lists for each block.
  • Short explanation of how CFG would be used in an optimisation if asked.

Quick check

  1. What are the three rules to identify leaders in an instruction stream?
  2. Given a block that ends with if cond goto L, how many successors does the block have and why?
  3. In a CFG, what is a back edge and why is it important for loops?

Answers: 1) (a) first instruction, (b) any target of a jump, (c) any instruction immediately following a jump. 2) Two successors: one to the target label (taken) and one to the fall-through next block (not-taken), unless there is no next instruction. 3) A back edge is an edge from a block to a dominator block earlier in flow (commonly to a loop header); it indicates a possible loop and is used to detect natural loops and do loop optimisations.

Note for the reviewer: I have written definitions and examples according to standard compiler textbooks and exam style. See the "verify" array for specific claims you should check or correct.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?