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]

-O2 (register allocation?) bug


//
// Dear developers,
//
// the following piece of code demonstrates a bug in the optimization features.
// If compiled with -O2 or -O3, the vector sum is not properly evaluated:
//
// 21:28 boa_sts:~/bugs/gcc> /local/egcs-1.0.3-prerelease/bin/g++ -O2 ini.cc
// 21:28 boa_sts:~/bugs/gcc> a.out
//  initsum: 500 0 2
// 21:29 boa_sts:~/bugs/gcc> /local/egcs-1.0.3-prerelease/bin/g++ -g ini.cc
// 21:36 boa_sts:~/bugs/gcc> a.out
//  initsum: 500 500 3
//
// The comments in the program text show circumstances under which the
// bug shows up/disappears (looks like register optimization (???) stuff to me)
//
// If XTVec is declared to have two elements only 
// (with the implied changes in the i/o, summation operators), the bug 
// disappears. Strange enough, it also disappears when some lines of 
// code are added to the main program (see there).
//
// Similar problems are present in gcc-2.8.1, but they show up under 
// somewhat/much simpler circumstances - a bug report was/will be submitted
// to gnu.g++.bugs.... 
//
// version info:
//21:36 boa_sts:~/bugs/gcc> uname -a
//SunOS boa.ica1.uni-stuttgart.de 5.5 Generic_103093-10 sun4u sparc SUNW,Ultra-1
//21:39 boa_sts:~/bugs/gcc> /local/egcs-1.0.3-prerelease/bin/g++ -v
//Reading specs from /local/egcs-1.0.3-prerelease/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.90.28/specs
//gcc version egcs-2.90.28 980423 (egcs-1.0.3 prerelease)
//  21:39 boa_sts:~/bugs/gcc> 
//
//  KUTGW -- Stefan.
//
//Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658 
//Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
//Pfaffenwaldring 27              pgp public key available on request/finger 
//70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts
//

#include <iostream.h>

class XTVec{
protected:
  double x[3];
public:
  XTVec(){x[0]=x[1] =x[2] =0;}
  XTVec(double ax,double y=0,double z=0){x[0]=ax;x[1]=y; x[2]=z; }
  double& operator[](int);
};

// comment this inline  
//  to make the bug dissappear when compiling with -O2: 
//  (with -O3 the bug is up and running...)
inline 
double & XTVec::operator[](int i){
  return x[i];
}

// comment this inline 
//  to make the bug dissappear when compiling with -O2:
//  (with -O3 the bug is up and running...)
inline 
XTVec& operator+=(XTVec& lhs, XTVec& rhs){
  lhs[0]+=rhs[0];
  lhs[1]+=rhs[1];
  lhs[2]+=rhs[2];
  return lhs;
}

inline 
XTVec operator+(XTVec& lhs, XTVec& rhs){
  XTVec result(lhs);
  return result += rhs;
  // replace the preceeding line with the two following to 
  //  make the bug dissappear 
  // result += rhs;
  // return result;
}

ostream& operator<<(ostream& out,XTVec& rhs){
  out << rhs[0] << " " << rhs[1]  << " " << rhs[2];
  return out;
}

int main()
{
  XTVec ur(500.,0.,2.);
  XTVec ll(0.,500.,1.);
 
  XTVec initsum(ur + ll);
  // uncomment to make the bug disappear:
  //XTVec sum;
  //sum[0] = ur[0] + ll[0];
  //sum[1] = ur[1] + ll[1];
  //sum[2] = ur[2] + ll[2];
  //cout << "     sum: " << sum << endl;

  cout << " initsum: " << initsum << endl;
  return 1;
}










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