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