This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/32856] Invalid optimization in the face of aliasing



------- Comment #6 from vogel at pi2 dot physik dot uni-erlangen dot de  2007-07-23 10:08 -------
This program demonstrates the problem, it creates different output depending on
if compiled with or without optimisation.

Without optimisation, n->next is not cached:
n->next = 0xbfb01af0
n->next = 0xbfb01af8

With optimisation, n->next is cached, this is wrong:
n->next = 0xbfdb3da0
n->next = 0xbfdb3da0

Note that the pointer c will point exactly one pointer-width above the
structure a, so n->next->next->prev=n -- which corresponds to c->prev=n -- will
overwrite n->next with n.

#include <stdio.h>

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

void foo(struct node* n) {
  printf("n->next = %p\n",n->next);
  n->next->next->prev=n;
  printf("n->next = %p\n",n->next);
};

int main(int argc,char **argv){
        struct node a = { },b = { };
        struct node *c = NULL;

        c = ((void*)&(a.next)) - sizeof(void*);
        b.next = c;
        a.next = &b;

        foo(&a);
}


-- 

vogel at pi2 dot physik dot uni-erlangen dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vogel at pi2 dot physik dot
                   |                            |uni-erlangen dot de


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]