Basic blocks and local optimisation
A local optimisation works inside one basic block. It must preserve the values observable after the block and every required side effect, even if an algebraic rewrite looks shorter.
7.1 Leaders, blocks and control-flow graphs
Past papers · CS 2014–2026 (23 papers): 4 questions, 8 marks
A basic block is a maximal straight-line instruction sequence: entry is at its first instruction and control transfers out only at its end. Its internal instructions need not each be represented as a separate graph node.
A block partition with a loop and a disconnected, unreachable block. Dashed outline marks the unreachable block.
When counting nodes/edges, state whether artificial entry and exit nodes are included. A control-flow graph may contain cycles and multiway branches; an AST describes nesting instead. A sequence of independent, feasible two-way decisions has syntactic paths; correlated conditions can reduce feasible paths and loops can give infinitely many execution paths. Count the paths requested by the question, not merely the branch instructions.
7.2 DAGs and local value numbering
Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks
A block DAG has leaves for incoming variable values and constants, and interior nodes for computations. Variable labels identify the node holding their current value. Assignment moves a label; it does not change what an old value node means.
For x = y op z, first find the current nodes for , reuse an existing node with the same operation and operand values if valid, then attach x to that result and remove its old current-value label. Reading operands must precede updating the target, especially in x=x+1.
The value DAG for E21. The multiply has two operand positions referring to the shared add node.
A DAG shares identical computations; it is not automatically a complete algebraic simplifier. Under exact integer arithmetic, (u-v)+v can simplify to u, enabling more sharing than structural CSE alone. For floating-point arithmetic, reassociation and cancellation can change rounding, overflow or exceptional behaviour. State the permitted algebra before claiming a globally minimum DAG.
An array/pointer store may invalidate previous loads through aliases. Unknown calls may change memory or have observable effects. Reuse a load only if no intervening operation can modify the location and repeated evaluation is otherwise unobservable. Distinguish an array's base address from its contents.
7.3 Local transformations and their validity
Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark
| Transformation | What changes | Required check |
|---|---|---|
| Constant folding | Evaluate a constant operation at compile time | Match the target arithmetic and exceptional behaviour |
| Constant propagation | Replace a variable use by its proven constant | All relevant incoming paths agree |
| Copy propagation | After x=y, substitute y for a use of x | Neither relevant value has changed; respect aliasing |
| Common subexpression elimination | Reuse a previous computed value | Operands and memory dependencies unchanged on every required path |
| Dead assignment elimination | Remove a definition whose value is not needed | RHS has no required side effect or trap |
| Unreachable-code elimination | Remove code that cannot execute | Reachability proof is sound |
| Strength reduction | Replace an expensive operation by a cheaper equivalent | Equivalence holds for types, ranges and machine behaviour |
| Peephole optimisation | Rewrite a small instruction window | Preserve flags, registers, memory and control effects that are live |
Loop-invariant computations can be moved out only if dependencies remain stable and the changed execution frequency is safe. In particular, a loop may execute zero times; hoisting a possible trap can introduce a new observable event. For operation-count questions, count setup once, body work per executed iteration, and loop-control work if requested. CSE can reuse stable loads across iterations only when alias/call effects have been ruled out. These are validity checks for the covered optimisations, not a general loop-transformation catalogue.
7.4 Expression register demand
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
The following compact result handles past questions about generating code for a pure expression tree. It assumes a load/store machine, freely selectable subtree evaluation order, no spilling, and a binary instruction whose destination may overwrite a consumed source register. Global register allocation additionally depends on interference and calling conventions.
Do not assign every literal a zero label and blindly apply the binary rule: an instruction still needs a destination, and a constant-only subtree may instead fold. For an immediate operation on an already computed register value, no additional operand register is needed. A machine requiring a distinct destination or fixed registers needs a modified calculation.
Last-minute recall
- Every jump target is a leader; so is the instruction after a jump.
- Partitioning and reachability elimination are different steps.
- Include artificial graph nodes only under the stated convention.
- DAG labels track current values; old operand values remain nodes.
- Redundant expressions require unchanged operands and memory dependencies.
- Dead results do not justify removing side effects.
- Register counts depend on the instruction model and evaluation order.