public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Correct ordering of write operations
@ 2008-02-19  3:07 Ryan Lortie
  2008-02-19 14:05 ` John Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Ryan Lortie @ 2008-02-19  3:07 UTC (permalink / raw)
  To: GCC Help List

Hello.

I have the following program:

volatile int z;
int q;

void
a (void)
{
  q = 42;
  z = 2;
  q = 7;
}

gcc -std=c99 -O3 -fomit-frame-pointer -S produces this code:

a:
        movl    $2, z
        movl    $7, q
        ret

I believe that the output should include setting 'q' to '42'.  Of
course, making 'q' volatile fixes that.

I'm reading §5.1.2.3 of the ISO C99 specification here.

In paragraph 2 it states that "modifying an object" (even a non-volatile
one) is a "side effect".  In paragraph 3 it says that "an actual
implementation need not evaluate part of an expression if it can
deduce ... that no needed side effects are produced" (but in this case,
a side effect -is- produced).  Finally, in paragraph 4 it says when
processing is interrupted by receipt of a signal that "the values of
objects as of the previous sequence point may be relied on".

Assume that 'z' and 'q' started out as zero.  A signal can occur at any
time.  The original code has 4 relevant sequence points in it:


============== here: z = 0, q = 0
  q = 42;
============== here: z = 0, q = 42
  z = 2;
============== here: z = 2, q = 42
  q = 7;
============== here: z = 2, q = 7

By my reading, this means that no signal handler should be able to see
values of z = 2 and q = 0.  There is no point in the program where such
values might be in those variables.

But of course, that is exactly what happens in this case:

a:
        movl    $2, z
			<---- signal happens here
        movl    $7, q
        ret

By the C99 specification I believe I shouldn't need to use volatile in
this case, but by experimenting with GCC it appears that I do.  Based on
my reading, my understanding tells me that GCC is diverging from the
specification here.

Can someone please explain to me why my understanding is incorrect or
why GCC chooses to diverge?

Cheers

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Correct ordering of write operations
  2008-02-19  3:07 Correct ordering of write operations Ryan Lortie
@ 2008-02-19 14:05 ` John Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2008-02-19 14:05 UTC (permalink / raw)
  To: Ryan Lortie, GCC-help

Hi Ryan,

The compiler correctly deduced that the side-effect of q = 42; is not
needed.

Making q volatile tells the compiler that the side-effect of q = 42; cannot
be determined to not be needed.

> By the C99 specification I believe I shouldn't need to use volatile in
> this case, but by experimenting with GCC it appears that I do.  Based on
> my reading, my understanding tells me that GCC is diverging from the
> specification here.

GCC is compliant with the specification, at least on this issue.

> Can someone please explain to me why my understanding is incorrect or
> why GCC chooses to diverge?

The compiler can fully analyze the use of q, and can determine that q = 42;
is a not needed side-effect, since q is not accessed between there and the
subsequent q = 7;.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-02-19 14:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-19  3:07 Correct ordering of write operations Ryan Lortie
2008-02-19 14:05 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).