โ† Compiler Design learning path
Compiler Design ยท Module 8 of 10

Code optimisation

Making generated code faster and smaller.

๐Ÿ“– Key ideas

  1. Optimisations must preserve program meaning.
  2. Local: constant folding (2 * 3 becomes 6), common subexpression elimination, dead code elimination.
  3. Loop: code motion (move invariant code out), strength reduction (multiply to add), loop unrolling.
  4. Basic blocks and control flow graphs organise code for analysis.
  5. Data-flow analysis (liveness, reaching definitions) tells which optimisations are safe.
๐Ÿ’ก Real-life example: Optimisation is like planning errands: buy everything from the same shop in one trip instead of going back for each item.
Constant foldingComputing constant expressions at compile time
Dead code eliminationRemoving code whose result is never used
Loop-invariant code motionMoving unchanging computations out of loops
Basic blockStraight-line code with one entry and one exit