public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/32856]  New: Invalid optimization in the face of aliasing
@ 2007-07-22 17:06 felix-gcc at fefe dot de
  2007-07-22 17:09 ` [Bug c/32856] " felix-gcc at fefe dot de
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: felix-gcc at fefe dot de @ 2007-07-22 17:06 UTC (permalink / raw)
  To: gcc-bugs

This contrived example is miscompiled by gcc:

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

void foo(struct node* n) {
  n->next->next->prev=n;
  n->next->prev->next=n;
}

This is not from real code, but I wrote it to demonstrate aliasing issues for a
talk I'm preparing now.  gcc -O2 -fno-strict-aliasing generates this code:

        movq    (%rdi), %rdx
        movq    (%rdx), %rax
        movq    %rdi, 8(%rax)
        movq    8(%rdx), %rax
        movq    %rdi, (%rax)

Note how rdx is used to cache the value of n->next.  Since we write through
some pointer that might alias other memory, gcc can not assume n still points
to the same value and n->next is unchanged after the first assignment.

Interestingly enough, changing assignments to

  n->next->next->next=n;
  n->next->prev->next=n;

properly reloads n->next.  I'm guessing that's because ->next has offset 0
relative to the pointer.


-- 
           Summary: Invalid optimization in the face of aliasing
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: felix-gcc at fefe dot de
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


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


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

end of thread, other threads:[~2007-07-25  8:24 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-22 17:06 [Bug c/32856] New: Invalid optimization in the face of aliasing felix-gcc at fefe dot de
2007-07-22 17:09 ` [Bug c/32856] " felix-gcc at fefe dot de
2007-07-22 17:12 ` felix-gcc at fefe dot de
2007-07-22 19:27 ` [Bug middle-end/32856] " pinskia at gcc dot gnu dot org
2007-07-22 23:01 ` falk at debian dot org
2007-07-22 23:08 ` felix-gcc at fefe dot de
2007-07-22 23:19 ` falk at debian dot org
2007-07-23 10:09 ` vogel at pi2 dot physik dot uni-erlangen dot de
2007-07-23 14:17 ` falk at debian dot org
2007-07-24 12:09 ` rguenth at gcc dot gnu dot org
2007-07-25  8:24 ` falk at debian 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).