Bug 33450 - Optimization with -O3 and -O2 producing bad code
Summary: Optimization with -O3 and -O2 producing bad code
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: unknown
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-09-16 21:23 UTC by Pierre-Alexandre Voye
Modified: 2007-09-17 06:46 UTC (History)
2 users (show)

See Also:
Host: x86 and x86_64-linux-pc-gnu
Target: x86 and x86_64-linux-pc-gnu
Build: x86 and x86_64-linux-pc-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Preprocessed file that show the bug (6.92 KB, application/octet-stream)
2007-09-16 21:26 UTC, Pierre-Alexandre Voye
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pierre-Alexandre Voye 2007-09-16 21:23:51 UTC
I have tested this code with many version of GCC, three under Linux x86 PC and one into x86 Mac OS X.
This bug appears with version 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9), in a Gentoo x86_64 Linux PC, and with Gcc 4.1.2 (Ubuntu 4.1.2-0ubuntu4) and 4.1.2 20061115 (prerelease) (Debian 4.1.1-21).
It doesn't appear in x86 Mac Os X 10.4 with "version 4.0.1 (Apple Computer, Inc. build 5250)"

This very simple code show the gcc's bug.

#include <stdio.h> // for printf

struct ELT {
  int item;
} one_elt;


int main()
{ struct ELT *two_elt;
  long *src,*dst;

  
  two_elt = malloc(sizeof(struct ELT));

  
  src = (long *)&one_elt;
  dst = (long *)two_elt;

  dst[0] = src[0];

  two_elt->item = 27;

  malloc(2);

  printf("27 = %d\n",two_elt->item);
  return( 0);
}

When you compile with -O1 or -O0, we get "27=27", but when we compile with -O2 or -O3 switch, we get "27=0"
Gcc "forget" code's dependancy.
Comment 1 Pierre-Alexandre Voye 2007-09-16 21:26:46 UTC
Created attachment 14215 [details]
Preprocessed file that show the bug
Comment 2 Eric Botcazou 2007-09-17 06:46:17 UTC
> It doesn't appear in x86 Mac Os X 10.4 with "version 4.0.1 (Apple Computer,
> Inc. build 5250)"

Because the GCC released by Apple defaults to -fno-strict-aliasing.

> Gcc "forget" code's dependancy.

No, your code violates the aliasing rules of the ISO C standard and GCC is
allowed to optimize it.  Either fix your code or use -fno-strict-aliasing.