Compilers have a ton of hard optimization passes, and many of those are solved well enough via stochastic algorithms. Nobody actually fully optimizes register allocation or whatever, and a few random restarts can go a long way toward some better performance.
No compiler does optimal register allocation, because optimal register allocation is an NP-complete problem.
Chaitin et al.: "Register allocation via coloring".
https://en.wikipedia.org/wiki/Register_allocation