Bug or feature of the level 2 C optimizer?

Robert Lansdale lansd at dgp.toronto.edu
Thu Mar 14 16:45:17 AEST 1991


	For the longest while I've have had problems with longjmp()'s
and level 2 optimizations in my rendering system. If an error occured
during the loading of a script file then the program would go into an
endless loop re-reading the first line of the file. The problem was
non-existent for the non-optimized version.

	A few printf's finally narrowed the problem down to a small
area of the code. The problem is best exemplified with the following
piece of code which I wrote to recreate the problem:

=========================================================================

/* A small program to demonstrate a level 2 optimization problem/bug. */
/* By Rob Lansdale, March 14, 1991. lansd at dgp.toronto.edu */

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

#define		TRUE	1

jmp_buf 	start;
int		user_script_stacked;

main(argc, argv)
	int	argc;
	char	*argv[];
{
	char	*tempstr;
	char	*user_script_file = "test.file";

	setjmp(start);

	if (user_script_file != (char *) NULL) {
		tempstr = user_script_file;
		/* Set pointer to NULL so an abort will not try to read the script in again */
		/* vvvvvvv THIS STATEMENT IS THROWN AWAY BY OPTIMIZER vvvv */
		user_script_file = (char *) NULL;
		open_script_file(tempstr);
		user_script_stacked = TRUE;
	}
	
	/* This comment is needed so that the optimizer will check to see */
	/* whether 'user_script_file' is used somewhere in the remainder */
	/* of the procedure.  Since it isn't, the optimizer will trash the */
	/* 'user_script_file = (char *) NULL' above. */
}

open_script_file(tempstr)
	char	*tempstr;
{
	printf("In open_script_file()\n");

	longjmp(start, 0); 
}

===========================================================================

	When this is run after being compiled with 'cc -O2', the program will 
repeatedly print out 'In open_script_file()'. The problem can be summarized 
as being due to the optimizer throwing away the 'user_script_file = (char *) 
NULL' statement since it is not used in the remainder of main(). While this 
is a valid optimization, it does not hold true for such cases as above where 
a longjmp() is used.

	Is this a bug or a feature of the optimizer? The problem can be
corrected by including a reference to the 'user_script_file' where the
4 line comment is.

--> Rob Lansdale

-- 
Robert Lansdale - (416) 978-6619       Dynamic Graphics Project	
Internet: lansd at dgp.toronto.edu        Computer Systems Research Institute
UUCP:   ..!uunet!dgp.toronto.edu!lansd University of Toronto
Bitnet:	  lansd at dgp.utoronto           Toronto, Ontario M5S 1A4, CANADA



More information about the Comp.sys.sgi mailing list