[Bug tree-optimization/56576] wrong code for aliased union at -O3

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat Mar 9 08:26:00 GMT 2013


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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-03-09 08:25:58 UTC ---
This is IMHO just wrong code.  C99 makes this undefined behavior of course, see
ISO C99, 6.2.6.1/7:
"When a value is stored in a member of an object of union type, the bytes of
the object representation that do not correspond to that member but do
correspond to other members take unspecified values, but the value of the union
object shall not thereby become a trap representation."
Now, while GCC allows some kind of type punning through unions, it doesn't
allow everything, in particular the access has to be done through the union,
not what you did, take pointers of the different union members and just access
everything through the pointers - if GCC wanted to support that, it would
simply has to give up on TBAA.  Also, among the unsupported things are e.g.
structure copies in between union members, consider say:
struct S { long l[256]; };
union U { struct T { int i; struct S s; } t; struct S s; } u;
...
  u.s = u.t.s;
or:
  u.t.s = u.s;
or:
  struct S *p = &u.t.s;
  ...
  *p = u.s;
or:
  struct S *p = &u.s;
  ...
  *p = u.t.s;
will just not work properly (we generate memcpy even when there is overlap),
because generally valid structure copies are either same address or without any
overlap.



More information about the Gcc-bugs mailing list