[tree-ssa/mainline PATCH] fold "test"==0 into constant

Roger Sayle roger@eyesopen.com
Thu Nov 6 13:36:00 GMT 2003


Hi Jan,
> The patch bellow is based on merging tree_expr_nonnegative_p and RTL
> nonzero_address implementation so it catch these and slightly more
> sophisficated cases.

You need to guard some of these cases with checks for "! flag_wrapv".
The code in rtlanal.c's nonzero_address_p is valid only because it is
only used for addressing arithmetic which has undefined overflow.  In
Java, for example, "max (x, 1) + max (y, 1)" can still be zero, even
though each term must be greater than zero.  [p.s. it looks like
you miss this case :>]  Similarly for multiplication where a zero result
can be generated from two non-zero operands.

> + 	else if (TREE_CODE (outer_type) == INTEGER_TYPE)
> + 	  {
> + 	    if (TREE_CODE (inner_type) == REAL_TYPE)
> + 	      return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
> + 	    if (TREE_CODE (inner_type) == INTEGER_TYPE)
> + 	      return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
> + 		      && TREE_UNSIGNED (inner_type);
> + 	  }
> +       }
> +       break;
> +    case ADDR_EXPR:
> +       /* Weak declarations may link to NULL.  */

And a blank line after the break for consistency with the other cases.


Longer-term, I think what constant folding really needs are two
functions tree_expr_lower_bound and tree_expr_upper_bound, where we
can implement some form of interval arithmetic.  Then the two current
functions tree_expr_nonnegative_p and tree_expr_nonzero_p can easily
be implemented by testing the returned bounds against zero.  This will
provide for more accurate implementations, but also enable many more
optimizations, for example, comparisons against any integer constant,
or comparisons between expressions with non-overlapping bounds.

For example, we could remove bounds checks from "switch (x & 7)"...

Roger
--



More information about the Gcc-patches mailing list