User account creation filtered due to spam.

Bug 2911 - Invalid C optimization on structs (regression)
Summary: Invalid C optimization on structs (regression)
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2001-05-23 05:46 UTC by bonachea
Modified: 2005-06-05 09:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bonachea 2001-05-23 05:46:01 UTC
The problematic code is shown below - 
on gcc 2.95.2 (any optimization level) it
correctly outputs:                                                                        
                                                                                
0 0 0x12341234                                                                  
0 0 0x12341234                                                                  
0 0 0x12341234                                                                  
0 0 0x12341234   

But on gcc 2.96 or the May 5th CVS snapshot of gcc
(compiled with -O2 or -O3), it instead outputs:                         
                                                                                
0 0 0x00000000                                                                  
0 0 0x00000000                                                                  
0 0 0x12341234                                                                  
0 0 0x00000000                                                                  

Apparently the optimizer is being overly aggressive in
reordering operations and as a result is generating 
incorrect code.

Release:
gcc version 2.96 20000731 (Red Hat Linux 7.0), also gcc version 3.1 20010507 (experimental)

Environment:
Red Hat Linux 7.0

How-To-Repeat:
compile code below as:
 gcc -O2 structcopy2.c -o structcopy2
on gcc 2.96 or newer and run the resulting executable
=========================================
#include <stdio.h>

typedef
struct {
  short x;
  short y;
  int *z;
} P;

typedef
struct {
  short x;
  short y;
  void *z;
} Q;

P p = {0,0,(int*)0};
int *SL = (int *)0x12341234;

void foo(P p1, P p2) {
 printf("%i %i 0x%08x\n",p1.x,p1.y,p1.z);  // output contents of p1 and p2
 printf("%i %i 0x%08x\n",p2.x,p2.y,p2.z);
}

int main() {

 P p1 = p;
 P p2 = p;

 // set one of p1's fields in an indirect way
 ((Q*)&p1)->z = (void *)SL;
 //(*(Q*)(&p1)).z = (void *)SL; // also fails
 //*((void **)(&(p1.z))) = (void *)SL; // also fails

 p2 = p1;  // p1 and p2 should be identical after this statement

 foo(p1, p2);

 printf("%i %i 0x%08x\n",p1.x,p1.y,p1.z);  // output contents of p1 and p2
 printf("%i %i 0x%08x\n",p2.x,p2.y,p2.z);
}
Comment 1 bonachea 2001-05-23 05:46:01 UTC
Fix:
Code of this form appears frequently in our software, and
this regression in the optimizer causes our system to 
break with gcc 2.96 or newer.
 
Our current workaround is to only use -O1, but performance
is an important goal for us so we'd really like to get this
fixed so we can go back to using -O3. 

Thanks.
Comment 2 Joseph S. Myers 2001-05-23 09:11:45 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug; your program violates ISO C aliasing rules by
    accessing an object as both an "int *" and a "void *".
    If you can't fix your program, use -fno-strict-aliasing.
    (Note that GCC does allow type punning through unions.)
Comment 3 Joseph S. Myers 2001-05-23 16:11:45 UTC
From: jsm28@gcc.gnu.org
To: jsm28@cam.ac.uk, bonachea@cs.berkeley.edu, gcc-gnats@gcc.gnu.org,
  nobody@gcc.gnu.org, titanium-devel@cs.berkeley.edu
Cc:  
Subject: Re: optimization/2911
Date: 23 May 2001 16:11:45 -0000

 Synopsis: Invalid C optimization on structs (regression)
 
 State-Changed-From-To: open->closed
 State-Changed-By: jsm28
 State-Changed-When: Wed May 23 09:11:45 2001
 State-Changed-Why:
     Not a bug; your program violates ISO C aliasing rules by
     accessing an object as both an "int *" and a "void *".
     If you can't fix your program, use -fno-strict-aliasing.
     (Note that GCC does allow type punning through unions.)
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=2911&database=gcc
Comment 4 Andrew Pinski 2005-06-05 09:30:25 UTC
Reopening to ...
Comment 5 Andrew Pinski 2005-06-05 09:30:45 UTC
Mark as a dup of bug 21920.

*** This bug has been marked as a duplicate of 21920 ***