Surprisingly safer languages can perform such optimisations without UB.
Usually it means GC, bound checking and no pointer forging.
Plenty of literature on the matter, some retro computing fun, or using some of the survivors in modern platforms, for example Ada.