Bug 33249 - [4.1 regression] -O3 and -O2 give wrong results
Summary: [4.1 regression] -O3 and -O2 give wrong results
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: 4.2.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-08-30 14:23 UTC by satyakaam Goswami
Modified: 2008-11-30 12:06 UTC (History)
3 users (show)

See Also:
Host:
Target: sparc-sun-solaris2.*
Build:
Known to work: 3.4.4 4.2.4 4.3.3 4.4.0
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description satyakaam Goswami 2007-08-30 14:23:58 UTC
The following code gives different results with -O3 (or -O2) and no optimization.The -O3 output is wrong. 

try:

$g++ -O3 test.cpp

$cat test.cpp

#include <iostream>

class foo
{
  int _type;
public:
  foo( int t ) : _type(t) {};
  bool is_1()	{ return _type == 1; }
  bool is_23()	{ return _type == 2 || _type == 3; }
};

void testit( foo& e1, foo& e2 )
{
  if ( e1.is_1() && e2.is_1() )
    std::cout << "If - 1.1\n";
  else if ( e1.is_23() && e2.is_23() )
    std::cout << "If - 23.23\n";
  else if ( e1.is_1() && e2.is_23() )
    std::cout << "If - 1.23\n";
  else if ( e1.is_23() && e2.is_1() )
    std::cout << "If - 23.1\n";
  else
    std::cout << "If - bad\n";
}

int main()
{
  for ( int i = 1; i < 4; ++i )
    for ( int j = 1; j < 4; ++j )
    {
      foo e1(i);
      foo e2(j);

      testit( e1, e2 );
    }

  return 0;
}


result with -O3 : 
$./a.out
If - 1.1
If - 1.23
If - 1.23
If - 23.1
If - 23.23
If - 23.23
If - bad
If - 23.23
If - 23.23

result without optimization :
$./a.out
If - 1.1
If - 1.23
If - 1.23
If - 23.1
If - 23.23
If - 23.23
If - 23.1
If - 23.23
If - 23.23

satya
Comment 1 Richard Biener 2007-08-30 14:43:36 UTC
This works for me on x86_64 and i686.  Possibly a rtl-optimization or target bug.
As there were loop fixes after 4.1.2 went out can you try with the trunk of
the gcc-4_1-branch or give the exact version of your compiler (gcc --version
will tell)?
Comment 2 satyakaam Goswami 2007-09-01 13:26:21 UTC
it is gcc 4.1.2 the base release.

Satya
Comment 3 Eric Botcazou 2008-11-30 12:06:32 UTC
Works fine with GCC 4.2.x and later.