gcc-2.5.2 g++ -O2, Exception Handling, program runs incorrectly

Krakowczyk, Andreas Andreas.Krakowczyk@fujitsu-siemens.com
Wed Sep 27 01:17:00 GMT 2000


Hi,
the following C++ program produces wrong output with option '-O2' or '-O3'.
Compiler version is
	g++ version 2.95.2 19991024 (release)		both case43 and
case45 fail
or	g++ version 2.96 20000918 (experimental)	only case43 fails

The compilers have been produced and installed on a Siemens mips machine
(system mips-sni-sysv4).
Same results occur on a pc (i686-linux-gnu).

Compile time options are plain '-O' (tests run o.k.) or '-O2' (tests fail).

Kind regards,

Andreas Krakowczyk
FSC EP LP AD C++
Andreas.Krakowczyk@fujitsu-siemens.com

This is the test program, with all header files removed, so it is the same
as the preprocessor output:


/********************************************************************/
/*                                                                  */
/*    COPYRIGHT (C) SIEMENS NIXDORF INFORMATIONSSYSTEME AG 1996     */
/*    COPYRIGHT (C) Fujitsu Siemens Computers GmbH         2000     */
/*                  ALL RIGHTS RESERVED                             */
/*                                                                  */
/********************************************************************/
/*                                                                  */
/*    FILE:         test_case43.c, test_case45.c                    */
/*                                                                  */
/*    AUTHOR:       Dr.Stadel / Salje                               */
/*                                                                  */
/*    CREATED:      1996-05-10                                      */
/*                                                                  */
/*    MODIFIED:     2000-09-26, A.Krakowczyk for GNU gcc bug report */ 
/*                                                                  */
/*    CONTENTS:     program for testing interaction between         */
/*                  exception-handling and optimization             */
/*                                                                  */
/*    test 43 :     loop transformation (strength reduction)        */
/*                  throw ---> catch                                */
/*                                                                  */
/*    test 45 :     loop transformation (eqntott optimization)      */
/*                  throw ---> catch                                */
/*                                                                  */
/********************************************************************/

extern "C" { extern int      printf(const char *, ...); }

bool  ok;
int   count;
int   e;                    // error status
int   a [100];
short sa [100], sb [100];

void count_throw (void);

bool test_case43 (int zero = 0, int one = 1);
bool test_case45 (int zero = 0, int one = 1);

int main (int argc, char *argv [])
{
   int fail = 0;
  
// Loop Transformations
  
   if (! test_case43 ())   { printf ("case43 failed\n"); fail += 1; }
   if (! test_case45 ())   { printf ("case45 failed\n"); fail += 1; }
  
   if (!fail)
	printf ("%-24s passed\n", argv[0]);
    else
	printf ("%-24s failed, %d\n", argv[0], fail);
   return 0;
}

bool test_case43 (int zero, int one)
{
   int i;

   ok    = 0;
   count = 0;

   for (i=0; i<100; i++)
      a [i] = zero;

   try
   {
      for (i=0; i<100; i++)
      {
         a [i] = one * i;
         count_throw ();
      }
   }

   catch (...)
   {
      ok = ((i == 2) && (a[0] == 0) && (a[1] == 1) && (a[2] == 2));
      for (i=3; i<100; i++)
         if (a[i] != 0)
            ok = 0;
   }

   return (ok);
}

bool test_case45 (int zero, int one)
{
   int aaa, bbb, i;

   ok    = 0;
   count = 0;

   for (i=0; i<100; i++)
      sa [i] = sb [i] = zero;
   sa [7] = sa [24] = sa [57] = one;

   try
   {
      for (i=0; i<100; i++)
      {
         aaa = sa [i];
         bbb = sb [i];
         if (aaa == bbb)
            goto m;
         if (aaa == bbb + one)
            count_throw ();
         m: ;
      }
   }

   catch (...)
   {
      ok = (i == 57);
   }

   return (ok);
}

void count_throw ()
{
   if (++count == 3)
   {
      throw "exception";
      e = 1;                      // error status set 
   }
}



More information about the Gcc-bugs mailing list