> It's not the code that it's harmful, but the transformation the compiler
> does in the optimization.
No. The code is the specification of what the programmer wants to happen. If the programmer doesn't mark something "volatile" when it should be, there's no way the compiler can automatically infer that information.
> Because there's no way it can be sure if the transformation is safe (or
> not!), it should not be doing it.
You've just killed all compiler optimization.
> Isn't it common sense that any code compiled today _can_ be used in a
> multi-threaded environment?
And any variable might be shared. So now the compiler must assume everything is volatile.