[Bug rtl-optimization/57359] wrong code for union access at -O3 on x86_64-linux

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue May 21 18:33:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57359

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I think this testcase is invalid.
C/C++ just disallow type punning through unions altogether (only one union
member can be active at each point), while GCC allows it as an extension, it
requires the accesses being done through the union, not through pointers to
individual union fields.
Quoting info gcc:
"     The practice of reading from a different union member than the one
     most recently written to (called "type-punning") is common.  Even
     with `-fstrict-aliasing', type-punning is allowed, provided the
     memory is accessed through the union type.  So, the code above
     will work as expected.  *Note Structures unions enumerations and
     bit-fields implementation::.  However, this code might not:
          int f() {
            union a_union t;
            int* ip;
            t.d = 3.0;
            ip = &t.i;
            return *ip;
          }
".  But that is exactly what the testcase does, you have a pointer to u.ll and
a pointer to u.i and use them interleaved.  The code would be valid if la
elements and k were pointers to the union and ppll pointer to pointer to the
union, and
accessed the i or ll fields in there.



More information about the Gcc-bugs mailing list