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]

optimization/2911: Invalid C optimization on structs (regression)



>Number:         2911
>Category:       optimization
>Synopsis:       Invalid C optimization on structs (regression)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed May 23 05:46:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Dan Bonachea
>Release:        gcc version 2.96 20000731 (Red Hat Linux 7.0), also gcc version 3.1 20010507 (experimental)
>Organization:
>Environment:
Red Hat Linux 7.0
>Description:

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.
>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);
}
>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.
>Release-Note:
>Audit-Trail:
>Unformatted:


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