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]
Other format: [Raw text]

[Bug tree-optimization/30493] New: [4.1 Regression] Unexpected compilation results: -O versus no optimization


The following C code demonstrates incorrect results when any optimization
is used:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <complex.h>
#include <math.h>

struct fs {
        int order;
        int numpoles;
        unsigned int polemask;
        complex data[100];
};

void choosepole(complex z, struct fs *flt)
{
  static int i=0;
  if (creal(z) < 0.0)
  {
    if (flt->polemask & 1)
    flt->numpoles++;
    flt->polemask >>= 1;
  }
  flt->data[i++]=z;
}

int main(int argc, char *argv[])
{
  struct fs flt;
  int i;
  flt.order=3;
  flt.numpoles=0;
  flt.polemask=(~0);
  memset(flt.data,0,100*sizeof(complex));
  for (i=0;i<2*flt.order;i++)
  {
    /* Following line is optimized incorrectly!!! */
    double theta = (flt.order&1) ?
              (i*M_PI)/flt.order :
              ((i+0.5)*M_PI)/flt.order;
    choosepole(cexp(I*theta),&flt);
  }
  for (i=0;i<2*flt.order;i++)
  {
    fprintf(stderr,"flt.data[%d]=%lg + I * %lg\n",
              i,
              creal(flt.data[i]),
              cimag(flt.data[i]));
  }
  return EXIT_SUCCESS;
}




+++ This bug was initially created as a clone of Bug #30088 +++

The following C++ program (preprocessed source attached) produces unexpected
results when compiled with -O1 -fstrict-aliasing (as opposed to -O1 only) or
with any higher level of optimization (-O2 or -O3).  No compilation warnings
are emitted in any of the cases.


#include <cassert>
#include <string>

struct A
{
  A() : _x(0.0), _y(0.0) { }
  float& f(const int& i) { return (i == 0 ? _x : _y); }
  float _x, _y;
};

struct B
{
  B(const char* s) : _s(s) { }
  std::string g() { return std::string(_s); }
  const char* _s;
};

A h(const char* s)
{
  if (s == 0)
    return A();
  A a;
  B b(s);
  if (b.g().compare(std::string("")) == 0)
    a.f(0) = a.f(1) = 1.0;
  else if (b.g().compare(std::string("")))
    b.g().compare(std::string(""));
  return a;
}

int main()
{
  A a(h(""));
  assert(a._x > 0.0 && a._y > 0.0);
  return 0;
}


Compilation results:

$ g++ -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../src/configure --prefix=/opt/gcc-4.1.1
Thread model: posix
gcc version 4.1.1
$ g++ -W -Wall -O1 testcase.cc      
$ ./a.out
$ g++ -W -Wall -O1 -fstrict-aliasing testcase.cc
$ ./a.out
a.out: testcase.cc:34: int main(): Assertion `a._x > 0.0 && a._y > 0.0' failed.
Aborted


The assertion ceases to fail (with -O1 -fstrict-aliasing) when adding any
(single one) of the options -fno-tree-copy-prop, -fno-tree-dce,
-fno-tree-salias or -fno-inline to the compiler command line.


-- 
           Summary: [4.1 Regression] Unexpected compilation results: -O
                    versus no optimization
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sam at sambromley dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30493


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