• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Three-address code is a flat sequence of instructions, but optimizations need to reason about program structure. A program has branches and joins — different paths of execution. Before analysis can begin, the compiler must partition TAC into straight-line segments where control enters at the top and leaves at the bottom, with no internal branches.

Core Idea

A basic block is a sequence of consecutive three-address code instructions with a single entry point (the first instruction) and a single exit point (the last instruction). No jumps enter or leave the block except at the entry and exit. Within a basic block, if one instruction executes, all execute. Basic blocks form the nodes of the control-flow graph.

How It Works

Basic blocks are identified by finding leaders — the first instruction of a block. Leaders are: the first instruction of the program, any instruction that is the target of a jump, and any instruction immediately following a jump. Starting from each leader, the block extends until another leader is reached (or the end). The block includes all instructions from the leader up to (but not including) the next leader.

Visual Explanation

basic_blocks TAC TAC: 1: t1 = a + b 2: t2 = c * d 3: if t1 < t2 goto L 4: t3 = t1 - t2 5: goto M L: 6: t3 = t2 - t1 M: 7: result = t3 BB1 Basic Block B1: 1: t1 = a + b 2: t2 = c * d TAC->BB1 BB2 Basic Block B2: 3: if t1 < t2 goto L BB1->BB2 BB3 Basic Block B3: 4: t3 = t1 - t2 5: goto M BB2->BB3 false BB4 Basic Block B4: 6: t3 = t2 - t1 BB2->BB4 true (goto L) BB5 Basic Block B5: 7: result = t3 BB3->BB5 BB4->BB5

Semantic Network

semantic_bb THIS Basic Blocks PRE1 Three-Address Code THIS--PRE1 built from — TAC partitioned into blocks OUT1 Control Flow Graph THIS--OUT1 builds into — blocks are CFG nodes OUT2 Data Flow Analysis THIS--OUT2 builds into — DFA operates on blocks REL1 Code Optimization THIS--REL1 related — optimizations work per block REL2 Intermediate Code Generation THIS--REL2 related

Key Properties

  • Single entry, single exit: Control enters at the top and leaves at the bottom
  • Leader-based identification: Partition TAC by finding leaders (first instruction, jump targets, post-jump instructions)
  • Sequential execution: If the first instruction executes, all instructions execute (no internal branches)
  • Optimization unit: Many optimizations (constant propagation, dead code elimination) operate within a single basic block
  • CFG nodes: Basic blocks are the vertices of the control-flow graph

Connections

Edge Cases & Gotchas

  • Empty blocks: A leader may be immediately followed by another leader, creating an empty basic block — rare but possible
  • Overlapping blocks: Blocks cannot overlap — each instruction belongs to exactly one block
  • Critical edges: Edges from a block with multiple successors to a block with multiple predecessors are called critical edges — they complicate optimization
  • Unreachable code: Instructions after an unconditional jump (but before the next leader) are unreachable — dead code elimination can remove them