---
title: "Code Generation and What an Optimiser May Change"
description: "The back end turns the intermediate form into instructions, assigns registers, and rearranges work. Every rearrangement is allowed only if the observable behaviour stays the same, which is why an opti"
canonical: https://lightmysky.com/learn/computing/code-generation-and-what-an-optimiser-may-change-mt__ZSPwP4hfA
source: https://lightmysky.com/learn/computing/code-generation-and-what-an-optimiser-may-change-mt__ZSPwP4hfA.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Code Generation and What an Optimiser May Change

The back end turns the intermediate form into instructions, assigns registers, and rearranges work. Every rearrangement is allowed only if the observable behaviour stays the same, which is why an optimiser can reorder arithmetic but not a write to a device.

Subject: Computing · Area: Computer Systems · Ages 21 to 22
Page: https://lightmysky.com/learn/computing/code-generation-and-what-an-optimiser-may-change-mt__ZSPwP4hfA

## Ready when they can

- Hand-generate instructions for a small expression and assign registers to the temporaries
- Name two transformations an optimiser is allowed to make and say what justifies each
- Explain why an optimiser must not remove a read that looks unused

## Lesson: Rearrange, but change nothing you can see

You already know the compiler lowers a tree to a simpler middle form. The back end turns that form into real instructions. It picks instructions, assigns registers to temporaries, and rearranges work to run faster.

**Example.** For a times b plus c, the back end loads each value into registers, multiplies the first two, then adds the third. With only two free registers it spills one value to memory and reloads it, since every temporary needs a home.

Every rearrangement must preserve observable behaviour: what the program prints, what it writes to devices, and what it returns. Reordering two independent sums is allowed because nobody can see a difference. Folding constants is allowed for the same reason.

**Tip.** Reads and writes that the outside world sees are fenced off. The optimiser must not remove a read that looks unused if a device sits behind it, and must never reorder across a visible write.

**Recap.** The back end assigns registers and reorders freely, but only where no observer could notice.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Semantic Analysis and the Intermediate Representation](https://lightmysky.com/learn/computing/semantic-analysis-and-the-intermediate-representation-mt_f2z_WQxfiw)
- [Race Conditions and Mutual Exclusion](https://lightmysky.com/learn/computing/race-conditions-and-mutual-exclusion-mt_hygT74jJDE)
- [Instruction Set Architecture as a Contract](https://lightmysky.com/learn/computing/instruction-set-architecture-as-a-contract-mt_pbwHRePY5M)
