https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61502
--- Comment #12 from Harald van Dijk <harald at gigawatt dot nl> ---
(In reply to Richard Biener from comment #11)
> I can't reproduce your findings with any of the specified GCC version nor
> with any other I tried (I tried on x86_64-linux and x86_64-linux with -m32).
> The test program always prints "x = 2, y = 2" as expected.
The wrong code should be visible by inspecting the generated assembly, but it
only actually fails at run-time if y directly follows x in memory. It did for
me back when I commented, but it no longer does. Here is a version that should
fail more reliably, by having only x and y as global variables, and by covering
both the case where y immediately follows x and the case where x immediately
follows y:
#include <stdio.h>
#include <string.h>
int x, y;
int main()
{
int *volatile v;
int *p;
v = &y;
p = v;
x = 2, y = 1;
if (p == &x + 1)
*p = 2;
else
y = 2;
printf ("x = %d, y = %d\n", x, y);
v = &x;
p = v;
x = 2, y = 1;
if (p == &y + 1)
*p = 1;
else
x = 1;
printf ("x = %d, y = %d\n", x, y);
return 0;
}
The only correct output is "x = 2, y = 2" followed by "x = 1, y = 1". On my
main system, I get "x = 2, y = 1" followed by "x = 1, y = 1". On another, I get
"x = 2, y = 2" followed by "x = 2, y = 1".