public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/48809] New: switch statement optimization error
@ 2011-04-28 18:32 andrewcmartin at msn dot com
  2011-04-28 18:35 ` [Bug c++/48809] " andrewcmartin at msn dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: andrewcmartin at msn dot com @ 2011-04-28 18:32 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-05-04  9:43 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
2011-04-28 18:35 ` [Bug c++/48809] " andrewcmartin at msn dot com
2011-04-28 18:36 ` andrewcmartin at msn dot com
2011-04-28 18:39 ` andrewcmartin at msn dot com
2011-04-28 18:54 ` [Bug c++/48809] [4.4/4.5/4.6/4.7 Regression] " jakub at gcc dot gnu.org
2011-04-29 13:40 ` jakub at gcc dot gnu.org
2011-04-30  6:55 ` jakub at gcc dot gnu.org
2011-04-30  7:01 ` jakub at gcc dot gnu.org
2011-04-30  7:36 ` [Bug c++/48809] [4.4/4.5 " jakub at gcc dot gnu.org
2011-05-03 16:43 ` [Bug testsuite/48809] " jakub at gcc dot gnu.org
2011-05-03 17:21 ` [Bug testsuite/48809] [4.4 " jakub at gcc dot gnu.org
2011-05-04  9:24 ` jakub at gcc dot gnu.org
2011-05-04  9:43 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).