No Sane Compiler Would Optimize Atomics
I love the WTF potential of this one:<p><i>Another interesting optimization is to use potentially shared memory locations (on the stack, heap and globals) as scratch storage, if the compiler can prove that they are not accessed in other threads concurrently. [..] For example the following transformation could occur:</i><p><pre><code>  &#x2F;&#x2F; Some code, but no synchronization.
  *p = 1; &#x2F;&#x2F; Can be on stack, heap or global.
</code></pre>
<i>Becomes:</i><p><pre><code>  &#x2F;&#x2F; ...
  *p = RAX; &#x2F;&#x2F; Spill temporary value.
  &#x2F;&#x2F; ...
  RAX = *p; &#x2F;&#x2F; Restore temporary value.
  &#x2F;&#x2F; ...
  *p = 1;
</code></pre>
<i>Since we write to p and there is no synchronization operations, other threads do not read&#x2F;write p without exercising undefined behavior. We can therefore use it as scratch storage.</i>