public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/56576] New: wrong code for aliased union at -O3
@ 2013-03-09  1:54 dhazeghi at yahoo dot com
  2013-03-09  8:26 ` [Bug tree-optimization/56576] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: dhazeghi at yahoo dot com @ 2013-03-09  1:54 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 56576
           Summary: wrong code for aliased union at -O3
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: dhazeghi@yahoo.com


The following code behaves differently at -O3 than at -O2 or below on 4.7 and
mainline on x86_64-linux.  At -O3 it returns 1, whereas at -O2 and below it
returns 0, for both -m32 and -m64 targets.

It behaves as expected on gcc 4.6 at all optimization levels (returns 0).

$ gcc-trunk --version
gcc-trunk (GCC) 4.8.0 20130308 (experimental) [trunk revision 196555]
$ gcc-trunk -O2 trans-reduced.c 
$ ./a.out 
$ echo $?
0
$ gcc-trunk -O3 trans-reduced.c 
$ ./a.out 
$ echo $?
1
$ gcc-4.6 -O3 trans-reduced.c 
$ ./a.out 
$ echo $?
0
$ cat trans-reduced.c 

/* gcc-4.7/gcc-trunk -O3 -m32/-m64 */
union
{
    int f0;
    int f1;
    long f2;
}
a, b;
int c, h;
int *d, *e = &a.f0;
long *f = &b.f2;
int **g = &d;
void fn1 ()
{
    c = 0;
    for (; c <= 3; c++)
    {
        int *i = &b.f1;
        *f = 1;
        *i = 0;
        *g = 0;
        h = *e;
    }
}

int main ()
{
    fn1 ();
    return b.f0;
}


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

* [Bug tree-optimization/56576] wrong code for aliased union at -O3
  2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
@ 2013-03-09  8:26 ` jakub at gcc dot gnu.org
  2013-03-10 21:17 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-03-09  8:26 UTC (permalink / raw)
  To: gcc-bugs


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.


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

* [Bug tree-optimization/56576] wrong code for aliased union at -O3
  2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
  2013-03-09  8:26 ` [Bug tree-optimization/56576] " jakub at gcc dot gnu.org
@ 2013-03-10 21:17 ` pinskia at gcc dot gnu.org
  2024-06-02 16:10 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-03-10 21:17 UTC (permalink / raw)
  To: gcc-bugs


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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-03-10 21:17:29 UTC ---
You cannot use union to avoid aliasing rules for normal accesses.


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

* [Bug tree-optimization/56576] wrong code for aliased union at -O3
  2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
  2013-03-09  8:26 ` [Bug tree-optimization/56576] " jakub at gcc dot gnu.org
  2013-03-10 21:17 ` pinskia at gcc dot gnu.org
@ 2024-06-02 16:10 ` pinskia at gcc dot gnu.org
  2024-06-02 16:11 ` pinskia at gcc dot gnu.org
  2024-06-02 16:12 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-02 16:10 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tangyixuan at mail dot dlut.edu.cn

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 115320 has been marked as a duplicate of this bug. ***

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

* [Bug tree-optimization/56576] wrong code for aliased union at -O3
  2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
                   ` (2 preceding siblings ...)
  2024-06-02 16:10 ` pinskia at gcc dot gnu.org
@ 2024-06-02 16:11 ` pinskia at gcc dot gnu.org
  2024-06-02 16:12 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-02 16:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 56577 has been marked as a duplicate of this bug. ***

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

* [Bug tree-optimization/56576] wrong code for aliased union at -O3
  2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
                   ` (3 preceding siblings ...)
  2024-06-02 16:11 ` pinskia at gcc dot gnu.org
@ 2024-06-02 16:12 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-02 16:12 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msl0000023508 at gmail dot com

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 93298 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2024-06-02 16:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-09  1:54 [Bug tree-optimization/56576] New: wrong code for aliased union at -O3 dhazeghi at yahoo dot com
2013-03-09  8:26 ` [Bug tree-optimization/56576] " jakub at gcc dot gnu.org
2013-03-10 21:17 ` pinskia at gcc dot gnu.org
2024-06-02 16:10 ` pinskia at gcc dot gnu.org
2024-06-02 16:11 ` pinskia at gcc dot gnu.org
2024-06-02 16:12 ` pinskia at gcc dot gnu.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).