A basic block is a maximal sequence of instructions with no jumps except at the end and no jump targets except at the beginning. Identifying basic blocks is the first step toward understanding program structure for optimization. Basic blocks form nodes of a control-flow graph.
Build a basic block graph from 3AC code and study how it represents program structure. Implement a basic block builder and test on loop-heavy code.
Exception handlers complicate basic block analysis (they do; must decide how to handle them). All instructions within a block can be reordered (only if they have no dependencies).
You already know that compilers translate source code into intermediate representations like three-address code (quadruples). But a flat list of instructions is hard to reason about for optimization — you need structure. A basic block is a maximal straight-line sequence of instructions where control flow enters only at the first instruction and exits only at the last. There are no jumps into the middle and no jumps out of the middle. If any instruction in the block executes, they all execute, in order.
The algorithm to identify basic blocks is straightforward. First, identify leaders — instructions that begin a new block. An instruction is a leader if it is the first instruction in the program, it is the target of any branch or jump, or it immediately follows a branch or jump. Each leader starts a new basic block that extends to (but does not include) the next leader. Applied to your intermediate representation, this partitioning transforms a flat instruction list into a structured collection of blocks, each with a single entry point and a single exit point.
Once you have basic blocks, you connect them to form the control-flow graph (CFG) you studied as a prerequisite. Each block becomes a node. An edge from block A to block B means that after A's last instruction executes, control might flow to B — either through a fall-through (sequential execution) or an explicit branch. The CFG is the foundational data structure for nearly every compiler optimization and analysis: liveness analysis, reaching definitions, dominance, and loop detection all operate on the CFG rather than on raw instruction lists.
Within a single basic block, optimization is relatively simple because execution is guaranteed to be sequential. Local optimizations like common subexpression elimination, constant folding, and dead code elimination can be applied within a block by analyzing instruction dependencies without worrying about control flow. The real power comes when you combine basic block structure with the CFG to perform global optimizations — analyses that reason across block boundaries about how data flows through the entire function. Basic block identification is therefore the essential first step: it decomposes the complexity of a whole program into manageable pieces that can be analyzed both individually and in relation to each other.