public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
[parent not found: <bug-14319-1165@http.gcc.gnu.org/bugzilla/>]
* [Bug c/14319] New: incorrect optimization of union of structs with common initial sequences
@ 2004-02-27  5:29 jsturm at gcc dot gnu dot org
  2004-05-27  5:21 ` [Bug rtl-optimization/14319] " pinskia at gcc dot gnu dot org
  2005-09-26 15:42 ` pinskia at gcc dot gnu dot org
  0 siblings, 2 replies; 10+ messages in thread
From: jsturm at gcc dot gnu dot org @ 2004-02-27  5:29 UTC (permalink / raw)
  To: gcc-bugs

Given:

struct X {int x;};
struct Y {int y;};
                                                                                
union U {struct X x; struct Y y;};
                                                                                
int f(struct X *xp, struct Y *yp) {
    xp->x = 0;
    yp->y = 1;
    return xp->x;
}
                                                                                
int main(void) {
    union U u;
    return f(&u.x, &u.y);
}

gcc from tree-ssa decides that xp->x and yp->y do not conflict, despite the
presence of a union.  Compiled with -O2, this program returns 1 for mainline, 0
for tree-ssa.

See discussion in http://gcc.gnu.org/ml/gcc/2004-02/msg01070.html and DR 257
(http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_257.htm).

6.5.2.3#5 reads:

    [#5] One special guarantee is made in order to simplify the use of unions:
if a union contains several structures that share a common initial sequence (see
below), and if the union object currently contains one of these structures, it
is permitted to inspect the common initial part of any of them anywhere that a
declaration of the complete type of the union is visible. Two structures share a
common initial sequence if corresponding members have compatible types (and, for
bit-fields, the same widths) for a sequence of one or more initial members.


The structs X and Y in the example above have a common initial sequence and are
contained in a visible union declaration, so depending on how one interprets the
statement in 6.5.2.3#5 (I am certainly not an expert) either the code example is
invalid, or the compiler must assume the members of X and Y can alias.

-- 
           Summary: incorrect optimization of union of structs with common
                    initial sequences
           Product: gcc
           Version: tree-ssa
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jsturm at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i686-pc-linux-gnu


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


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

end of thread, other threads:[~2021-07-29 18:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-14319-4@http.gcc.gnu.org/bugzilla/>
2015-04-26  0:15 ` [Bug rtl-optimization/14319] incorrect optimization of union of structs with common initial sequences pinskia at gcc dot gnu.org
2020-08-31 18:26 ` pinskia at gcc dot gnu.org
2020-08-31 18:49 ` fxcoudert at gcc dot gnu.org
2020-10-21  9:45 ` vincent-gcc at vinc17 dot net
2020-10-21 13:45 ` jameskuyper at alumni dot caltech.edu
2021-07-29 18:55 ` pinskia at gcc dot gnu.org
     [not found] <bug-14319-1165@http.gcc.gnu.org/bugzilla/>
2009-12-19  3:45 ` pinskia at gcc dot gnu dot org
2009-12-19 14:41 ` dvilleneuve at kronos dot com
2004-02-27  5:29 [Bug c/14319] New: " jsturm at gcc dot gnu dot org
2004-05-27  5:21 ` [Bug rtl-optimization/14319] " pinskia at gcc dot gnu dot org
2005-09-26 15:42 ` pinskia at gcc dot gnu dot org

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).