[patch] for PR 23361

Zdenek Dvorak rakdver@atrey.karlin.mff.cuni.cz
Thu Feb 8 16:14:00 GMT 2007


Hello,

> >one of the reasons for this PR is that we fail to fold expressions like
> >a + 10 > MIN_INT + 2, that are always true (assuming that the
> >arithmetics in the considered type does not overflow).  Such expressions
> >are fairly often produced by number of iterations analysis.
> >This patch implements such simplifications.
> >
> >Bootstrapped & regtested on i686 and x86_64.
> 
> Hmm, this looks overly complicated.  But maybe I'm missing something
> obvious - the
> attached also works for your testcase, but I dind't yet bootstrap & test it.

well, it seems to work (*).  I think you should also extend the comment
explaining how the code works -- it took me about half an hour before I
understood it.  Also please include the compare-3 testcase (it took me
longer time to write than the fold-const code :-)

Zdenek

(*) there are few typos (you miss the GE_EXPR there, and
omit_one_operand). 

> Index: fold-const.c
> ===================================================================
> *** fold-const.c	(revision 121711)
> --- fold-const.c	(working copy)
> *************** fold_comparison (enum tree_code code, tr
> *** 8043,8048 ****
> --- 8043,8071 ----
>   
>         lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
>   			 TREE_TYPE (arg1), const2, const1);
> + 
> +       /* If the constant operation overflowed this can be
> + 	 simplified as a comparison against INT_MAX/INT_MIN.  */
> +       if (TREE_CODE (lhs) == INTEGER_CST
> + 	  && TREE_OVERFLOW (lhs))
> + 	{
> + 	  int const1_sgn = tree_int_cst_sgn (const1);
> + 	  enum tree_code code2 = code;
> + 
> + 	  if (TREE_CODE (arg0) == MINUS_EXPR)
> + 	    const1_sgn = -const1_sgn;
> + 	  if (const1_sgn == -1)
> + 	    code2 = swap_tree_comparison (code);
> + 
> + 	  if (code2 == LT_EXPR
> + 	      || code2 == LE_EXPR
> + 	      || code2 == EQ_EXPR)
> + 	    return boolean_false_node;
> + 	  else if (code2 == NE_EXPR
> + 		   || code2 == GT_EXPR)
> + 	    return boolean_true_node;
> + 	}
> + 
>         if (TREE_CODE (lhs) == TREE_CODE (arg1)
>   	  && (TREE_CODE (lhs) != INTEGER_CST
>   	      || !TREE_OVERFLOW (lhs)))
> 



More information about the Gcc-patches mailing list