This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug middle-end/32856] Invalid optimization in the face of aliasing
- From: "vogel at pi2 dot physik dot uni-erlangen dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 23 Jul 2007 10:08:51 -0000
- Subject: [Bug middle-end/32856] Invalid optimization in the face of aliasing
- References: <bug-32856-3511@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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