public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/18806] New: generate wrong code with hi optimization
@ 2004-12-03 11:07 paolo dot abeni at tilab dot com
  2004-12-03 13:00 ` [Bug rtl-optimization/18806] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: paolo dot abeni at tilab dot com @ 2004-12-03 11:07 UTC (permalink / raw)
  To: gcc-bugs

compiling the following fragment:

typedef struct pseudoheader       /* pseudo header for TCP checksum calculations */
{
    unsigned int sip, dip;
    unsigned char  zero;
    unsigned char  protocol;
    unsigned short tcplen;
} ph_struct;
                                                                               
                                                       
static inline unsigned short in_chksum_tcp(  unsigned short *h)
{
   unsigned int cksum;
                                                                               
                                                       
   /* PseudoHeader must have 12 bytes */
   cksum  = h[0];
   cksum += h[1]; /* [1] */
   cksum += h[2];
   cksum += h[3];
   cksum += h[4];
   cksum += h[5];
   return cksum;
}
                                                                               
                                                       
int main()
{
    unsigned short a, b;
                                                                               
                                                       
    ph_struct ph;
    union pseudoheader_union{
                                                                               
                                                       
        ph_struct ph;
        unsigned short a[6];
    } p;
                                                                               
                                                       
    ph.sip = 0xa0a44d4; /* [2] */
    ph.dip = 0xf50ac610;
    ph.zero = 0;
    ph.protocol = 0x06;
    ph.tcplen = 1216;
    a = in_chksum_tcp(&ph);
                                                                               
                                                       
    p.ph.sip = 0xa0a44d4;
    p.ph.dip = 0xf50ac610;
    p.ph.zero = 0;
    p.ph.protocol = 0x06;
    p.ph.tcplen = 1216;
    b = in_chksum_tcp(p.a);
    printf("CHKSUM: %hx %hx %d\n", a, b, sizeof(ph));
    return 0;
}

with:

gcc -O2 

the value calulated in the first invocation of in_chksum_tcp function is wrong

this is NOT related to what is described in
http://gcc.gnu.org/bugs.html#nonbugs_c for breaking aliasing rules.

It seems that the compiler, while generating the assembly code, inverts an
assignment operation (marked with [2] in the c code fragment) with the
sum operation (marked with [1] in the c code fragment).

-- 
           Summary: generate wrong code with hi optimization
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P1
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: paolo dot abeni at tilab dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i586-madrake-linux-gnu
  GCC host triplet: i586-madrake-linux-gnu
GCC target triplet: i586-madrake-linux-gnu


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


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

* [Bug rtl-optimization/18806] generate wrong code with hi optimization
  2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
@ 2004-12-03 13:00 ` pinskia at gcc dot gnu dot org
  2004-12-03 13:47 ` dberlin at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-12-03 13:00 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-12-03 13:00 -------
Actually there was some discussion about this in the standard comittee recently.  Right now it is not 
clear what should happen.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |rtl-optimization
           Keywords|                            |wrong-code


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


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

* [Bug rtl-optimization/18806] generate wrong code with hi optimization
  2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
  2004-12-03 13:00 ` [Bug rtl-optimization/18806] " pinskia at gcc dot gnu dot org
@ 2004-12-03 13:47 ` dberlin at gcc dot gnu dot org
  2005-03-04  4:04 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: dberlin at gcc dot gnu dot org @ 2004-12-03 13:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From dberlin at gcc dot gnu dot org  2004-12-03 13:47 -------
Actually, this is related to the aliasing stuff described in the non-bugs.

If you look, in the first instance,  you are casting a structure to an unsigned
short *, and treating it like an array.

In fact, i get
structa.c: In function `main':
structa.c:46: warning: passing arg 1 of `in_chksum_tcp' from incompatible
pointer type

This is not legal code.

The second invocation, where you pass p.a, is correct, and we give you correct
code as a result.

If you compile the file with -fno-strict-aliasing, you also get the result you
want, since we ignore the fact that you are violating the aliasing rules.

-- 


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


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

* [Bug rtl-optimization/18806] generate wrong code with hi optimization
  2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
  2004-12-03 13:00 ` [Bug rtl-optimization/18806] " pinskia at gcc dot gnu dot org
  2004-12-03 13:47 ` dberlin at gcc dot gnu dot org
@ 2005-03-04  4:04 ` pinskia at gcc dot gnu dot org
  2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
  2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-04  4:04 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-03-04 04:04 -------
Invalid as you are violating aliasing rules.

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


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


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

* [Bug rtl-optimization/18806] generate wrong code with hi optimization
  2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
                   ` (3 preceding siblings ...)
  2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
@ 2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-06-05  9:09 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-06-05 09:09 -------
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=18806


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

* [Bug rtl-optimization/18806] generate wrong code with hi optimization
  2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
                   ` (2 preceding siblings ...)
  2005-03-04  4:04 ` pinskia at gcc dot gnu dot org
@ 2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
  2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-06-05  9:09 UTC (permalink / raw)
  To: gcc-bugs


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

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


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


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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-03 11:07 [Bug c/18806] New: generate wrong code with hi optimization paolo dot abeni at tilab dot com
2004-12-03 13:00 ` [Bug rtl-optimization/18806] " pinskia at gcc dot gnu dot org
2004-12-03 13:47 ` dberlin at gcc dot gnu dot org
2005-03-04  4:04 ` pinskia at gcc dot gnu dot org
2005-06-05  9:09 ` pinskia at gcc dot gnu dot org
2005-06-05  9:09 ` 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).