public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/39255]  New: bitfield alias generates incorrect assembly
@ 2009-02-20 23:45 jsworley at qwest dot net
  2009-02-20 23:48 ` [Bug c/39255] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jsworley at qwest dot net @ 2009-02-20 23:45 UTC (permalink / raw)
  To: gcc-bugs

Consider the following code:

typedef struct {
    unsigned long A:16;
    unsigned long B:48;
} bits;

int
wrong(unsigned long y)
{
    bits *p = (bits *) &y;

    if (y == 0 || p->A >= 512) {
        return (1);
    }

    return (0);
}

int
right(unsigned long y)
{
    bits *p = (bits *) &y;

    if (y == 0 || p->A >= 512) {
        return (-1);
    }

    return (0);
}

The *only* difference between right() and wrong() is the return value in the IF
clause. With -O2, the function right() compiles to correct, if suboptimal,
code. The function wrong(), however, generates the following:

wrong:
    .prologue
    .body
    .mmi
    cmp.ne p6, p7 = 0, r32
    nop 0
    addl r14 = 511, r0
    .mmi
    ld2 r15 = [r12]    <<<<<< HUH?
    st8 [r12] = r32    <<<<<< HUH?
    addl r8 = 1, r0
    .mib
    nop 0
    nop 0
    (p7) br.ret.dpnt.many rp
    ;;
    .mmi
    cmp4.gtu p6, p7 = r15, r14
    ;;wrong:
    .prologue
    .body
    .mmi
    cmp.ne p6, p7 = 0, r32
    nop 0
    addl r14 = 511, r0
    .mmi
    ld2 r15 = [r12]
    st8 [r12] = r32
    addl r8 = 1, r0
    .mib
    nop 0
    nop 0
    (p7) br.ret.dpnt.many rp
    ;;
    .mmi
    cmp4.gtu p6, p7 = r15, r14
    ;;
    (p6) addl r14 = 1, r0
    (p7) mov r14 = r0
    ;;
    .mib
    nop 0
    mov r8 = r14
    br.ret.sptk.many rp
    .endp wrong#

    (p6) addl r14 = 1, r0
    (p7) mov r14 = r0
    ;;
    .mib
    nop 0
    mov r8 = r14
    br.ret.sptk.many rp
    .endp wrong#

It is clearly wrong to perform the load from the stack before the proper value
(r32) has been stored. When run, the value of p->A is the low 16 bits of
whatever was stored on the stack before.


-- 
           Summary: bitfield alias generates incorrect assembly
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jsworley at qwest dot net
  GCC host triplet: i686-linux-gnu
GCC target triplet: ia64-linux-gnu


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


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

* [Bug c/39255] bitfield alias generates incorrect assembly
  2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
@ 2009-02-20 23:48 ` pinskia at gcc dot gnu dot org
  2009-02-20 23:56 ` pinskia 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 @ 2009-02-20 23:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2009-02-20 23:47 -------
-O2 enables strict aliasing (type based aliasing).  You need to use
-fno-strict-aliasing to disable that.


-- 


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


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

* [Bug c/39255] bitfield alias generates incorrect assembly
  2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
  2009-02-20 23:48 ` [Bug c/39255] " pinskia at gcc dot gnu dot org
@ 2009-02-20 23:56 ` pinskia at gcc dot gnu dot org
  2009-02-20 23:59 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-20 23:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2009-02-20 23:56 -------


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


-- 

pinskia at gcc dot gnu dot org changed:

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


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


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

* [Bug c/39255] bitfield alias generates incorrect assembly
  2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
  2009-02-20 23:48 ` [Bug c/39255] " pinskia at gcc dot gnu dot org
  2009-02-20 23:56 ` pinskia at gcc dot gnu dot org
@ 2009-02-20 23:59 ` pinskia at gcc dot gnu dot org
  2009-02-21  0:26 ` jsworley at qwest dot net
  2009-02-21  0:44 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-20 23:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2009-02-20 23:59 -------
Note in 4.4 and above, GCC generates "correct" even with strict aliasing
enabled and it generates better code, it does not store on the stack at all.


-- 


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


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

* [Bug c/39255] bitfield alias generates incorrect assembly
  2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
                   ` (2 preceding siblings ...)
  2009-02-20 23:59 ` pinskia at gcc dot gnu dot org
@ 2009-02-21  0:26 ` jsworley at qwest dot net
  2009-02-21  0:44 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: jsworley at qwest dot net @ 2009-02-21  0:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jsworley at qwest dot net  2009-02-21 00:25 -------
Whether or not this is just an aliasing bug, I'd like to understand why
changing the return value from 1 to -1 makes the compiler generate correct
code.


-- 

jsworley at qwest dot net changed:

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


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


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

* [Bug c/39255] bitfield alias generates incorrect assembly
  2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
                   ` (3 preceding siblings ...)
  2009-02-21  0:26 ` jsworley at qwest dot net
@ 2009-02-21  0:44 ` pinskia at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-02-21  0:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from pinskia at gcc dot gnu dot org  2009-02-21 00:44 -------
Because of different optimizations happen.  When having 1, it basically
creates:
if (y == 0)
  return p->A >= 512;
return 0;

with -1, it keeps the branch.

This is normal with aliasing violations really.

Anyways the code is undefined so the compiler technically could do anything.

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


-- 

pinskia at gcc dot gnu dot org changed:

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


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


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

end of thread, other threads:[~2009-02-21  0:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-20 23:45 [Bug c/39255] New: bitfield alias generates incorrect assembly jsworley at qwest dot net
2009-02-20 23:48 ` [Bug c/39255] " pinskia at gcc dot gnu dot org
2009-02-20 23:56 ` pinskia at gcc dot gnu dot org
2009-02-20 23:59 ` pinskia at gcc dot gnu dot org
2009-02-21  0:26 ` jsworley at qwest dot net
2009-02-21  0:44 ` 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).