public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
@ 2004-10-06 18:12 ` davidel at xmailserver dot org
  2004-10-06 18:13 ` davidel at xmailserver dot org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:12 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From davidel at xmailserver dot org  2004-10-06 18:12 -------
Created an attachment (id=7296)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=7296&action=view)
Test program


-- 


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


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

* [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
@ 2004-10-06 18:12 davidel at xmailserver dot org
  2004-10-06 18:12 ` [Bug c/17865] " davidel at xmailserver dot org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:12 UTC (permalink / raw)
  To: gcc-bugs

When compiling the simple program below using -O1 and -O{2,3}, you will get
different results.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define __HI(x) ((int *) &(x))[1]

double test(double v) {
        int hi;
        hi = __HI(v);
        return hi + v;
}

int main(int ac, char **av) {
        double v = 2.30912;
        if (ac > 1)
                v = atof(av[1]);
        printf("val=%lf\n", test(v));
        return 0;
}


Looking at the code generation you will notice that the offset of the "fildl" is
clearly wrong, being it accessing the uninitialized part of the stack (it should
have been "12(%ebp)"):

test:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        fildl   -4(%ebp)
        faddl   8(%ebp)
        leave
        ret

-- 
           Summary: Bad code generation (using -O2 and -O3) on x86 target
                    when using the listed snippet
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: davidel at xmailserver dot org
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: 3.3.2
GCC target triplet: 3.3.2


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
  2004-10-06 18:12 ` [Bug c/17865] " davidel at xmailserver dot org
@ 2004-10-06 18:13 ` davidel at xmailserver dot org
  2004-10-06 18:13 ` davidel at xmailserver dot org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From davidel at xmailserver dot org  2004-10-06 18:13 -------
Created an attachment (id=7298)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=7298&action=view)
Intermediate output


-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
  2004-10-06 18:12 ` [Bug c/17865] " davidel at xmailserver dot org
  2004-10-06 18:13 ` davidel at xmailserver dot org
@ 2004-10-06 18:13 ` davidel at xmailserver dot org
  2004-10-06 18:20 ` pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From davidel at xmailserver dot org  2004-10-06 18:13 -------
Created an attachment (id=7297)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=7297&action=view)
Code generated


-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (2 preceding siblings ...)
  2004-10-06 18:13 ` davidel at xmailserver dot org
@ 2004-10-06 18:20 ` pinskia at gcc dot gnu dot org
  2004-10-06 18:30 ` davidel at xmailserver dot org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-06 18:20 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-10-06 18:20 -------
You are violating aliasing rules.

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


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (3 preceding siblings ...)
  2004-10-06 18:20 ` pinskia at gcc dot gnu dot org
@ 2004-10-06 18:30 ` davidel at xmailserver dot org
  2004-10-06 18:34 ` pinskia at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:30 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From davidel at xmailserver dot org  2004-10-06 18:30 -------
Note that this code works with -O1, and it is part on the NetLib math library
that worked for ages. Now, there are many ways to achieve the same thing, but if
aliasing rules wants to be enforced, the should be enforced with every
optimization level, and not break with -O2/3 and work just fine with -O1. A lot
of existing C code alias values in this or similar way, and if this breaks other
stuff will too. 


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


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (4 preceding siblings ...)
  2004-10-06 18:30 ` davidel at xmailserver dot org
@ 2004-10-06 18:34 ` pinskia at gcc dot gnu dot org
  2004-10-06 18:41 ` bangerth at dealii dot org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-06 18:34 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-10-06 18:34 -------
Yes but -O2 enables -fstrict-aliasing which means follow the ANSI/ISO C
aliasing rules which you/the program is violating.
Use either -fno-strict-aliasing or an union which GCC defines (unlike the ANSI/
ISO C standard where it is undefined too).

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


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (5 preceding siblings ...)
  2004-10-06 18:34 ` pinskia at gcc dot gnu dot org
@ 2004-10-06 18:41 ` bangerth at dealii dot org
  2004-10-06 18:50 ` davidel at xmailserver dot org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: bangerth at dealii dot org @ 2004-10-06 18:41 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-10-06 18:41 -------
Andrew is right: some codes break because only higher optimization 
levels do certain aggressive transformations, and work when lower 
optimizations are used. There is not much we can do about it, apart 
from asking our users to fix their codes. The manual contains a 
section on aliasing problems, and how to avoid them. 
 
W. 

-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (6 preceding siblings ...)
  2004-10-06 18:41 ` bangerth at dealii dot org
@ 2004-10-06 18:50 ` davidel at xmailserver dot org
  2004-10-06 19:00 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: davidel at xmailserver dot org @ 2004-10-06 18:50 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From davidel at xmailserver dot org  2004-10-06 18:50 -------
Oh, I agree. But in this case a clear error instead of a warning would be
better. Error better fits the bad code generation, instead of a warning.


-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (7 preceding siblings ...)
  2004-10-06 18:50 ` davidel at xmailserver dot org
@ 2004-10-06 19:00 ` pinskia at gcc dot gnu dot org
  2004-10-06 19:03 ` bangerth at dealii dot org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-06 19:00 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-10-06 19:00 -------
It cannot be an error since this only undefined at runtime and not undefined
at compile time.

-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (8 preceding siblings ...)
  2004-10-06 19:00 ` pinskia at gcc dot gnu dot org
@ 2004-10-06 19:03 ` bangerth at dealii dot org
  2005-06-05  8:59 ` pinskia at gcc dot gnu dot org
  2005-06-05  9:01 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: bangerth at dealii dot org @ 2004-10-06 19:03 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-10-06 19:03 -------
Well, to be more precise: if we knew how to formulate the conditions under 
which we are sure that an aliasing violation happens, we'd be happy to  
provide clearer compiler diagnostics. However, this has been discussed many 
times on the mailing lists, and somehow nobody ever came up with a real 
good way to determine wether the aliasing rules are violated or not. 
 
So for the moment: we're sorry, but we don't know how to do that. Your 
code is invoking undefined behavior at runtime, and the standards allow 
the compiler to let this slip by with no diagnostic required. 
 
W. 

-- 


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (9 preceding siblings ...)
  2004-10-06 19:03 ` bangerth at dealii dot org
@ 2005-06-05  8:59 ` pinskia at gcc dot gnu dot org
  2005-06-05  9:01 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-06-05  8:59 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-06-05 08:59 -------
Reopening to ...

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


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


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

* [Bug c/17865] Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet
  2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
                   ` (10 preceding siblings ...)
  2005-06-05  8:59 ` pinskia at gcc dot gnu dot org
@ 2005-06-05  9:01 ` pinskia at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-06-05  9:01 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-06-05 08:59 -------
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***

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


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


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

end of thread, other threads:[~2005-06-05  9:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-06 18:12 [Bug c/17865] New: Bad code generation (using -O2 and -O3) on x86 target when using the listed snippet davidel at xmailserver dot org
2004-10-06 18:12 ` [Bug c/17865] " davidel at xmailserver dot org
2004-10-06 18:13 ` davidel at xmailserver dot org
2004-10-06 18:13 ` davidel at xmailserver dot org
2004-10-06 18:20 ` pinskia at gcc dot gnu dot org
2004-10-06 18:30 ` davidel at xmailserver dot org
2004-10-06 18:34 ` pinskia at gcc dot gnu dot org
2004-10-06 18:41 ` bangerth at dealii dot org
2004-10-06 18:50 ` davidel at xmailserver dot org
2004-10-06 19:00 ` pinskia at gcc dot gnu dot org
2004-10-06 19:03 ` bangerth at dealii dot org
2005-06-05  8:59 ` pinskia at gcc dot gnu dot org
2005-06-05  9:01 ` 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).