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]

Illegal optimization in presence of setjmp


Dear gcc-bugs,

In the small program below, the result obtained with no optimization
and with -O2 are different. Yet, no warning is issued whatsoever.

The test has been run on gcc 2.95.2, Solaris 5.6/7, i386 and sparc
architectures.

    
    ravel% gcc -Wall bug.c -o bug
    ravel% ./bug
    Test 1: i=0
    Test 2: i=1
    Test 3: i=1
    ravel% gcc -Wall -O2 bug.c -o bug
    ravel% ./bug
    Test 1: i=0
    Test 2: i=1
    Test 3: i=0

A preliminary examination of the generated assembly code shows that the
optimizer propagates the "i=0" assignment to the "else" branch of the
if. This is correct in general... but in the presence of setjmp-like
functions! Actually, in such a case, both the "then" and the "else"
branches can be successively followed.

Regards,

Luc Bouge.

_____________________________________________________
LIP, ENS Lyon, 46 Allee d'Italie, F-69364 Lyon Cedex 07, France.
Tel.: +33 (0)4 72 72 84 36, Fax: +33 (0)4 72 72 80 80,
mailto:Luc.Bouge@ens-lyon.fr, http://www.ens-lyon.fr/~bouge/

________________________________________________________________________

#include <stddef.h>
#include <stdio.h>
#include <setjmp.h>


#define TEST(n, i) \
	fprintf (stderr, "Test %d: i=%d\n", n, i);

static sigjmp_buf context;

int
main ()
{
    int i;
    i = 0;
    TEST (1, i);

    if (setjmp (context) == 0)
      {
	  i = 1;
	  TEST (2, i);
	  longjmp (context, 1);
      }
    else
	TEST (3, i);

    return 0;
}


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