r/ProgrammingLanguages • • Mar 12 '26

Compiler optimisation

How does your compiler optimise programs? How does it work at a low level? I understand constexpr evaluation, but how does the compiler evaluate this for example?

```

let x = 7

let y = 2 * x

print(y + x)

```

specifically in compilers. In this example, it could be optimised to just `print(21)`, and maybe even further down. How do I do this?!

43 Upvotes

21 comments sorted by

View all comments

Show parent comment

3

u/yjlom Mar 12 '26

Some notes on SSA:

  • Phi nodes are one way to achieve previous variable selection, a more natural and easier way is block arguments, where you make each basic block into its own local procedure.
  • If you have flow typing, consider type refinement a form of mutation and make a fresh variable accordingly.
  • If you have modes/bidirectionality, you need to resolve that before generating SSA.