Bug 50364 - Volatile miscompilation
Summary: Volatile miscompilation
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-12 09:08 UTC by Abramo Bagnara
Modified: 2011-10-13 08:43 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Abramo Bagnara 2011-09-12 09:08:05 UTC
$ cat p.cc
volatile int a, b;
void f() {
  b = 1 + (a = 0);
}
$ g++ -O2 -S p.cc
$ cat p.s
        .file   "p.cc"
        .text
        .p2align 4,,15
        .globl  _Z1fv
        .type   _Z1fv, @function
_Z1fv:
.LFB0:
        .cfi_startproc
        movl    $0, a
        movl    $1, b
        ret
        .cfi_endproc
...

As the typescript above shows, the conversion between lvalue (a = 0) and relative rvalue does not generate a volatile read memory access unlike what I think that standard mandates.
Comment 1 Paolo Carlini 2011-10-12 15:54:19 UTC
Doesn't look like a C++ front-end issue.
Comment 2 Richard Biener 2011-10-13 08:43:10 UTC
That works as designed.  See the recent discussion about this very topic
on the gcc mailinglist and

2010-08-19  Nathan Sidwell  <nathan@codesourcery.com>
            Richard Guenther  <richard.guenther@gmail.com>

        * gimplify.c (gimplify_modify_expr): When assigning to volatiles,
        copy the src value and return a copy.
        * doc/extend.texi (Volatiles): Move from C++ to C and expand.
        (C++ Volatiles): Adjust to describe C++ semantics only.

esp.

"Assignments are also expressions and have an rvalue.  However when
assigning to a scalar volatile, the volatile object is not reread,
regardless of whether the assignment expression's rvalue is used or
not.  If the assignment's rvalue is used, the value is that assigned
to the volatile object.  For instance, there is no read of @var{vobj}
in all the following cases:

@smallexample
int obj;
volatile int vobj;
vobj = @var{something};
obj = vobj = @var{something};
obj ? vobj = @var{onething} : vobj = @var{anotherthing};
obj = (@var{something}, vobj = @var{anotherthing});
@end smallexample

If you need to read the volatile object after an assignment has
occurred, you must use a separate expression with an intervening
sequence point."