Consider the following function.
void incr(_Atomic int *restrict ptr) {
*ptr += 1;
}
I'll consider x86, but my question is about the language, not the semantics of any particular implementation of atomics. GCC and Clang both emit the following:
incr:
lock add DWORD PTR [rdi], 1
ret
Is it possible for a conforming implementation to emit simply
incr:
add DWORD PTR [rdi], 1
ret
(The same thing you get if you remove _Atomic.)
Without restrict, this would be a miscompilation because add is not atomic, so (for instance) calling incr at the same time in two threads would race. However, since the pointer is restrict-qualified, I think it's not possible for a race to occur between incr and any other access to *ptr (without causing undefined behavior anyway).
I would feel confident making this optimization manually, but no compiler I know of does so automatically. Is it correct? Or have I misunderstood restrict?