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 c++/48809] New: switch statement optimization error


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

           Summary: switch statement optimization error
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: andrewcmartin@msn.com


Created attachment 24131
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24131
C++ source

The code below should return the answer "18".  However, built with g++ v4.5.2
and -O2, it returns "-386954936" or some other random value.  Removing the line
"case -62:..." from the switch statement should have no effect, but the
resulting optimized code will execute correctly.

---------------

#include <iostream>
int switch_test(signed char code)
{
  int val = 0;
  switch (code) {
    case   0: val = 1;  break;
    case   1: val = 2;  break;
    case   2: val = 3;  break;
    case   3: val = 4;  break;
    case   4: val = 5;  break;
    case   5: val = 6;  break;
    case   6: val = 7;  break;
    case   7: val = 8;  break;
    case   8: val = 9;  break;
    case   9: val = 10; break;
    case  10: val = 11; break;
    case  11: val = 12; break;
    case  12: val = 13; break;
    case  13: val = 14; break;
    case  14: val = 15; break;
    case  15: val = 16; break;
    case  16: val = 17; break;
    case  98: val = 18; break;
    case -62: val = 19; break;
  }
  return val;
}

int main(int argc, char* argv[])
{
  const char input = 98;
  int return_val = switch_test(input);
  std::cout << "input:  " << input << std::endl;
  std::cout << "return: " << return_val << std::endl;
}

---------------

Command line to trigger the error:

[amartin@jeanie:~]$ g++ -O2 -save-temps switch_test.cc && ./a.out
input:  b
return: -386954936

If we don't optimize we don't get the error:

[amartin@jeanie:~]$ g++ switch_test.cc && ./a.out
input:  b
return: 18


Compiler information:

[amartin@jeanie:~]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/libexec/gcc/x86_64-unknown-linux-gnu/4.5.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure
--prefix=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/
--with-gmp=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/
--with-mpfr=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/
--with-mpc=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/
--with-ppl=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/
--with-cloog=/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/ --enable-static
--enable-languages=c,c++,objc,obj-c++
LDFLAGS='-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib
-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib64
-I/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/include '
CXXFLAGS='-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib
-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib64
-I/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/include '
CFLAGS='-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib
-L/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/lib64
-I/apps/gcc/4.5.2/Linux_2.6.9_x86_64-dynamic/include'
Thread model: posix
gcc version 4.5.2 (GCC)

[amartin@jeanie:~]$ uname -a
Linux jeanie 2.6.9-78.0.22.ELsmp #1 SMP Thu Apr 30 19:17:40 EDT 2009 x86_64
x86_64 x86_64 GNU/Linux

I have also observed this error in g++ 4.4.x.


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