[PATCH] Fix middle-end/38661, ICE in tree-switch-conversion with INT_MAX as the case statement

Martin Jambor mjambor@suse.cz
Tue Dec 30 10:32:00 GMT 2008


Hi,

On Mon, Dec 29, 2008 at 07:47:31PM -0500, Andrew Pinski wrote:
> Hi,
>   The problem here is that if we have INT_MAX as a case statement, pos
> in build_constructors would wrap around which will cause an infinite
> loop.
> The fix is to change the loop into a do/while loop and look for the
> wrapping case.
> 
> OK? Bootstrapped and tested on i386-darwin8.11 with no regressions.
> 
> Thanks,
> Andrew Pinski
> 
> ChangeLog:
> * tree-switch-conversion.c (build_constructors): Test for wrapping of pos case.

I can't approve anything but since I wrote the original code perhaps I
should comment: This is certainly fine.

Thanks a lot,

Martin

> 
> 
> * gcc.c-torture/compile/pr38661.c: New testcase.
> * gcc.c-torture/compile/pr38661-1.c: New testcase.

> Index: tree-switch-conversion.c
> ===================================================================
> --- tree-switch-conversion.c	(revision 142951)
> +++ tree-switch-conversion.c	(working copy)
> @@ -440,9 +440,10 @@ build_constructors (gimple swtch)
>  	{
>  	  gimple phi = gsi_stmt (gsi);
>  	  tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
> +	  tree low = CASE_LOW (cs);
>  	  pos = CASE_LOW (cs);
>  
> -	  while (!tree_int_cst_lt (high, pos))
> +	  do 
>  	    {
>  	      constructor_elt *elt;
>  
> @@ -452,7 +453,7 @@ build_constructors (gimple swtch)
>  	      elt->value = val;
>  
>  	      pos = int_const_binop (PLUS_EXPR, pos, integer_one_node, 0);
> -	    }
> +	    } while (!tree_int_cst_lt (high, pos) && tree_int_cst_lt (low, pos));
>  	  j++;
>  	}
>      }
> Index: testsuite/gcc.c-torture/compile/pr38661-1.c
> ===================================================================
> --- testsuite/gcc.c-torture/compile/pr38661-1.c	(revision 0)
> +++ testsuite/gcc.c-torture/compile/pr38661-1.c	(revision 0)
> @@ -0,0 +1,9 @@
> +/* We used to ICE because we would wrap INT_MAX
> +   into INT_MIN while doing the switch converison. */
> +
> +const char *func(int val) {
> + switch (val) {
> +   case - __INT_MAX__ -1 : return "foo";
> +   default: return "";
> + }
> +}
> Index: testsuite/gcc.c-torture/compile/pr38661.c
> ===================================================================
> --- testsuite/gcc.c-torture/compile/pr38661.c	(revision 0)
> +++ testsuite/gcc.c-torture/compile/pr38661.c	(revision 0)
> @@ -0,0 +1,9 @@
> +/* We used to ICE because we would wrap INT_MAX
> +   into INT_MIN while doing the switch converison. */
> +
> +const char *func(int val) {
> + switch (val) {
> +   case __INT_MAX__: return "foo";
> +   default: return "";
> + }
> +}



More information about the Gcc-patches mailing list