https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69494

            Bug ID: 69494
           Summary: Optimizer eliminates assignment to volatile subobject
           Product: gcc
           Version: 5.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deaeod at gmail dot com
  Target Milestone: ---

Compiler options: -O1
Code: (http://goo.gl/W1Iubr)
#include <cstdlib>

struct device {
    volatile unsigned tcc;
};

void test_device() {
    device dev;
    unsigned tcc = rand();

    dev.tcc = tcc;
}

I expected GCC to generate code that accesses dev.tcc, but failed to find it. I
tested all versions and targets of GCC available at http://gcc.godbolt.org but
never did the compiler generate code for the assignment to dev.tcc at a level
of optimization higher than zero.

I think this is a bug because paragraph 12 of section [intro.execution] of the
C++14 standard specifies that accessing volatile glvalues of objects is a
side-effect, thus observable behavior.

GCC does warn about dev being unused but set, which is misleading as dev is not
being set, but dev.tcc is, and assigning to dev.tcc has side-effects.

Reply via email to