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

* [Bug c++/48809] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
@ 2011-04-28 18:35 ` andrewcmartin at msn dot com
  2011-04-28 18:36 ` andrewcmartin at msn dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andrewcmartin at msn dot com @ 2011-04-28 18:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from andrewcmartin at msn dot com 2011-04-28 18:33:40 UTC ---
Created attachment 24133
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24133
-save-temps .o file


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

* [Bug c++/48809] switch statement optimization error
  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
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andrewcmartin at msn dot com @ 2011-04-28 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from andrewcmartin at msn dot com 2011-04-28 18:33:10 UTC ---
Created attachment 24132
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24132
-save-temps .ii file


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

* [Bug c++/48809] switch statement optimization error
  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
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andrewcmartin at msn dot com @ 2011-04-28 18:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from andrewcmartin at msn dot com 2011-04-28 18:34:10 UTC ---
Created attachment 24134
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24134
-save-temps .s file


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

* [Bug c++/48809] [4.4/4.5/4.6/4.7 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (2 preceding siblings ...)
  2011-04-28 18:39 ` andrewcmartin at msn dot com
@ 2011-04-28 18:54 ` jakub at gcc dot gnu.org
  2011-04-29 13:40 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-04-28 18:54 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.04.28 18:50:45
                 CC|                            |jakub at gcc dot gnu.org
   Target Milestone|---                         |4.4.7
            Summary|switch statement            |[4.4/4.5/4.6/4.7
                   |optimization error          |Regression] switch
                   |                            |statement optimization
                   |                            |error
     Ever Confirmed|0                           |1

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-04-28 18:50:45 UTC ---
Confirmed, goes away with -fno-tree-switch-conversion.

extern void abort (void);

int
foo (signed char x)
{
  int y = 0;
  switch (x)
    {
    case 0: y = 1; break;
    case 1: y = 2; break;
    case 2: y = 3; break;
    case 3: y = 4; break;
    case 4: y = 5; break;
    case 5: y = 6; break;
    case 6: y = 7; break;
    case 7: y = 8; break;
    case 8: y = 9; break;
    case 9: y = 10; break;
    case 10: y = 11; break;
    case 11: y = 12; break;
    case 12: y = 13; break;
    case 13: y = 14; break;
    case 14: y = 15; break;
    case 15: y = 16; break;
    case 16: y = 17; break;
    case 98: y = 18; break;
    case -62: y = 19; break;
    }
  return y;
}

int
main ()
{
  signed char x = 98;
  if (foo (x) != 18)
    abort ();
}

Will look at it tomorrow.


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

* [Bug c++/48809] [4.4/4.5/4.6/4.7 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (3 preceding siblings ...)
  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
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-04-29 13:40 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |jakub at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-04-29 13:38:08 UTC ---
Created attachment 24145
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24145
gcc46-pr48809.patch

Untested fix.


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

* [Bug c++/48809] [4.4/4.5/4.6/4.7 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (4 preceding siblings ...)
  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
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-04-30  6:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-04-30 06:54:06 UTC ---
Author: jakub
Date: Sat Apr 30 06:54:02 2011
New Revision: 173207

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173207
Log:
    PR tree-optimization/48809
    * tree-switch-conversion.c (build_arrays): Compute tidx in unsigned
    type.
    (gen_inbound_check): Don't compute index_expr - range_min in utype
    again, instead reuse SSA_NAME initialized in build_arrays.
    Remove two useless gsi_for_stmt calls.

    * gcc.c-torture/execute/pr48809.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr48809.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-switch-conversion.c


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

* [Bug c++/48809] [4.4/4.5/4.6/4.7 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (5 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-04-30  7:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-04-30 06:55:15 UTC ---
Author: jakub
Date: Sat Apr 30 06:55:11 2011
New Revision: 173208

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173208
Log:
    PR tree-optimization/48809
    * tree-switch-conversion.c (build_arrays): Compute tidx in unsigned
    type.
    (gen_inbound_check): Don't compute index_expr - range_min in utype
    again, instead reuse SSA_NAME initialized in build_arrays.
    Remove two useless gsi_for_stmt calls.

    * gcc.c-torture/execute/pr48809.c: New test.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/gcc.c-torture/execute/pr48809.c
Modified:
    branches/gcc-4_6-branch/gcc/ChangeLog
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_6-branch/gcc/tree-switch-conversion.c


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

* [Bug c++/48809] [4.4/4.5 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (6 preceding siblings ...)
  2011-04-30  7:01 ` jakub at gcc dot gnu.org
@ 2011-04-30  7:36 ` jakub at gcc dot gnu.org
  2011-05-03 16:43 ` [Bug testsuite/48809] " jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-04-30  7:36 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[4.4/4.5/4.6/4.7            |[4.4/4.5 Regression] switch
                   |Regression] switch          |statement optimization
                   |statement optimization      |error
                   |error                       |

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-04-30 07:35:10 UTC ---
Fixed for 4.6+ so far.


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

* [Bug testsuite/48809] [4.4/4.5 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (7 preceding siblings ...)
  2011-04-30  7:36 ` [Bug c++/48809] [4.4/4.5 " jakub at gcc dot gnu.org
@ 2011-05-03 16:43 ` jakub at gcc dot gnu.org
  2011-05-03 17:21 ` [Bug testsuite/48809] [4.4 " jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-03 16:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-03 16:37:19 UTC ---
Author: jakub
Date: Tue May  3 16:37:12 2011
New Revision: 173328

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173328
Log:
    Backport from mainline
    2011-04-30  Jakub Jelinek  <jakub@redhat.com>

    PR tree-optimization/48809
    * tree-switch-conversion.c (build_arrays): Compute tidx in unsigned
    type.
    (gen_inbound_check): Don't compute index_expr - range_min in utype
    again, instead reuse SSA_NAME initialized in build_arrays.
    Remove two useless gsi_for_stmt calls.

    * gcc.c-torture/execute/pr48809.c: New test.

Added:
    branches/gcc-4_5-branch/gcc/testsuite/gcc.c-torture/execute/pr48809.c
Modified:
    branches/gcc-4_5-branch/gcc/ChangeLog
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_5-branch/gcc/tree-switch-conversion.c


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

* [Bug testsuite/48809] [4.4 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (8 preceding siblings ...)
  2011-05-03 16:43 ` [Bug testsuite/48809] " jakub at gcc dot gnu.org
@ 2011-05-03 17:21 ` 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
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-03 17:21 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[4.4/4.5 Regression] switch |[4.4 Regression] switch
                   |statement optimization      |statement optimization
                   |error                       |error

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-03 17:17:57 UTC ---
Fixed for 4.5.4+ too.


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

* [Bug testsuite/48809] [4.4 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (9 preceding siblings ...)
  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
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-04  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-04 09:19:10 UTC ---
Author: jakub
Date: Wed May  4 09:19:07 2011
New Revision: 173358

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173358
Log:
    Backport from mainline
    2011-04-30  Jakub Jelinek  <jakub@redhat.com>

    PR tree-optimization/48809
    * tree-switch-conversion.c (build_arrays): Compute tidx in unsigned
    type.
    (gen_inbound_check): Don't compute index_expr - range_min in utype
    again, instead reuse SSA_NAME initialized in build_arrays.
    Remove two useless gsi_for_stmt calls.

    * gcc.c-torture/execute/pr48809.c: New test.

Added:
    branches/gcc-4_4-branch/gcc/testsuite/gcc.c-torture/execute/pr48809.c
Modified:
    branches/gcc-4_4-branch/gcc/ChangeLog
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_4-branch/gcc/tree-switch-conversion.c


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

* [Bug testsuite/48809] [4.4 Regression] switch statement optimization error
  2011-04-28 18:32 [Bug c++/48809] New: switch statement optimization error andrewcmartin at msn dot com
                   ` (10 preceding siblings ...)
  2011-05-04  9:24 ` jakub at gcc dot gnu.org
@ 2011-05-04  9:43 ` jakub at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-04  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #12 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-04 09:33:21 UTC ---
Fixed.


^ 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).