User account creation filtered due to spam.

Bug 18806 - generate wrong code with hi optimization
Summary: generate wrong code with hi optimization
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.4.1
: P1 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: wrong-code
Depends on:
Reported: 2004-12-03 11:06 UTC by Paolo Abeni
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i586-madrake-linux-gnu
Target: i586-madrake-linux-gnu
Build: i586-madrake-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Paolo Abeni 2004-12-03 11:06:55 UTC
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; = 0;
    ph.protocol = 0x06;
    ph.tcplen = 1216;
    a = in_chksum_tcp(&ph);
                                                   = 0xa0a44d4; = 0xf50ac610; = 0; = 0x06; = 1216;
    b = in_chksum_tcp(p.a);
    printf("CHKSUM: %hx %hx %d\n", a, b, sizeof(ph));
    return 0;


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 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).
Comment 1 Andrew Pinski 2004-12-03 13:00:11 UTC
Actually there was some discussion about this in the standard comittee recently.  Right now it is not 
clear what should happen.
Comment 2 Daniel Berlin 2004-12-03 13:47:13 UTC
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.
Comment 3 Andrew Pinski 2005-03-04 04:04:17 UTC
Invalid as you are violating aliasing rules.
Comment 4 Andrew Pinski 2005-06-05 09:09:33 UTC
Reopening to ...
Comment 5 Andrew Pinski 2005-06-05 09:09:50 UTC
Mark as a dup of bug 21920.

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