gcc compiler bug (sparc, ppc)

Till Straumann strauman at slac.stanford.edu
Tue May 22 23:44:19 UTC 2007


I found that gcc produces bad code for the
following example:

struct node {
     struct node *next, *prev;
};

void xtract(struct node *x)
{
struct node *n, *p;
    n = x->n;
    p = x->p;
    n->prev = p;
    p->next = n;
}


powerpc-rtems-gcc -O -fschedule-insns -fno-strict-aliasing

(version 4.1.1) produces:

00000000 <xtract>:
   0:   81 63 00 04     lwz     r11,4(r3)
   4:   81 23 00 00     lwz     r9,0(r3)
   8:   91 2b 00 00     stw     r9,0(r11)
   c:   91 69 00 04     stw     r11,4(r9)
  10:   4e 80 00 20     blr

i.e.,

   p = x->p;
   n = x->n;
   p->next = n;
   n->prev = p;

The order of the last two assignments was swapped which
makes a difference in the special case where &p->next and &n->prev
are addressing the same location but n != p.

The bug is apparently triggered by -fschedule-insns; if I turn
on all other optimizations (including -fstrict-aliasing) correct
code is generated.

BTW, sparc-gcc-3.2.3 produces the same error (same gcc options as above)

00000000 <xtract>:
   0:   d2 02 20 04     ld  [ %o0 + 4 ], %o1
   4:   c2 02 00 00     ld  [ %o0 ], %g1
   8:   c2 22 40 00     st  %g1, [ %o1 ]
   c:   81 c3 e0 08     retl
  10:   d2 20 60 04     st  %o1, [ %g1 + 4 ]

Admittedly, the case where p->next aliases n->prev is unlikely
but this kind of bug hurts confidence in the tool :-(

-- T.





More information about the users mailing list