[gnu.gcc.bug] Bug in GCC 1.35 WRT to optimizing constant switch statements

meissner@DG-RTP.DG.COM (Michael Meissner) (08/15/89)

Ron Guilmette found a bug in the way GCC 1.35 handles optimizing
switch statements whose index is constant and negative.  The problem
is in stmt.c when the constant is converted to tree form, it is not
sign extended, so that later on, it won't match the appropriate case
label.  Here is a sample program that demonstrates this (you must
optimize it):

	int main ()
	{
		int num;

		switch (num = -1) {
		case -1:
			exit (0);
		case 0:
			exit (1);
		default:
			printf ("wrong case selected\n");
			exit (2);
		}
	}

Here is a patch to fix the problem (your line numbers will vary):

*** stmt.c.orig	Mon Aug 14 16:22:06 1989
--- stmt.c	Mon Aug 14 17:53:51 1989
***************
*** 2509,2515 ****
  		 if we don't already have one.  */
  	      if (TREE_CODE (index_expr) != INTEGER_CST)
  		{
! 		  index_expr = build_int_2 (INTVAL (index), 0);
  		  index_expr = convert (TREE_TYPE (index_expr), index_expr);
  		}
  
--- 2509,2516 ----
  		 if we don't already have one.  */
  	      if (TREE_CODE (index_expr) != INTEGER_CST)
  		{
! 		  index_expr = build_int_2 (INTVAL (index),
! 					    INTVAL (index) >= 0 ? 0 : -1);
  		  index_expr = convert (TREE_TYPE (index_expr), index_expr);
  		}