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]

Incorrect optimized code generated by current CVS snapshot



Using the 20000403/ CVS snapshot RPMs from
http://www.codesourcery.com/gcc-snapshots.html, the following code has
different behavior when compiled by the snapshot with "-O
-funroll-loops" than when compiled without optimization or compiled by
any other compiler yet tested:


  void f(int i, int j, int radius, int width, int N)
  {
    const int diff   = i-radius;
    const int lowk   = (diff>0 ? diff : 0 );
    int k;
  
    printf("k[%2d,%2d]\n",lowk,2);
    
    for(k=lowk; k<= 2; k++){
      int idx = ((k-i+radius)*width-j+radius);
      printf("          idx:%2d\n",idx);
    }
  
    for(k=lowk; k<= 2; k++);
  }
  
  
  int main(int argc, char **argv)
  {
    int exc_rad=2;
    int N=8;
    int i;
    for(i=1; i<4; i++)
      f(i,1,exc_rad,2*exc_rad + 1, N);
  } 


Here's the output from a working compiler on a Pentium III:

  linux> /usr/bin/cc -v
  Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.95.2/specs
  gcc version 2.95.2 19991024 (release)

  linux> /usr/bin/cc -O -funroll-loops -g bug6.c 

  linux> ./a.out
  k[ 0, 2]
  	    idx: 6
  	    idx:11
  	    idx:16
  k[ 0, 2]
  	    idx: 1
  	    idx: 6
  	    idx:11
  k[ 1, 2]
  	    idx: 1
  	    idx: 6

And here's the output from the current snapshot on the same machine;
notice the new negative idx:

  linux> /usr/local/bin/cc -v
  Reading specs from /usr/local/bin/../lib/gcc-lib/i386-pc-linux-gnu/2.96/specs
  gcc version 2.96 20000402 (experimental)

  linux> /usr/local/bin/cc -O -funroll-loops bug6.c 

  linux> ./a.out
  k[ 0, 2]
  	    idx: 6
  	    idx:11
  	    idx:16
  k[ 0, 2]
  	    idx: 1
  	    idx: 6
  	    idx:11
  k[ 0, 2]
  	    idx:-4
  	    idx: 1
  	    idx: 6

Thus for some unknown reason the CVS snapshot is generating the
incorrect lowk on some calls of f(), which causes the index to drop
below zero (which is a big no-no for the rest of my code ;-).

Should this code prove useful as a test case, I hereby place it into
the public domain for you or anyone else to use for whatever purpose
you or they see fit.

Jim Bednar

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