> The problem is that the compiler has *no way* to know if the code is potentially harmful
It's not the code that it's harmful, but the transformation the compiler does in the optimization. To put it another way: the compiler chooses to do something that is NOT what the code says. It does so because it assumes it's safe. Because there's no way it can be sure if the transformation is safe (or not!), it should not be doing it.
> The compiler doesn't know which parts of code will be operating in a multi-threaded environment
Isn't it common sense that any code compiled today _can_ be used in a multi-threaded environment?