On Thu, Feb 29, 2024 at 02:08:19PM +0100, Richard Biener wrote: > > So, wouldn't it be better to outline what you have above + POLY_INT_CST > > handling into a helper function, which similarly to get_range_pos_neg > > returns a bitmask, but rather than 1 bit for may be [0, max] and another > > bit for > > may be [min, -1] you return 3 bits, 1 bit for may be [1, max], another for > > may be [0, 0] and another for may be [min, -1]? > > Also, I bet you actually want to handle TREE_UNSIGNED just as [0, 0] > > and [1, max] ranges unlike get_range_pos_neg. > > I'm just lazy and given TYPE_OVERFLOW_WRAPS (and thus unsigned) doesn't > ever get here and I special-case integer_zerop it doesn't really matter > that in these cases get_range_pos_neg isn't exactly what's wanted - I'm > asking it only for those cases where it works just fine.
Just handling integer_zerop doesn't cover the case where the chrec operand isn't INTEGER_CST, just includes zero in its range. And I'd think that is something quite common (sure, INTEGER_CST chrec operands are likely more common than that) that we know that something isn't negative, or isn't positive, or is non-negative, or is non-positive etc. > > So perhaps > > int ret = 7; > > if (TYPE_UNSIGNED (TREE_TYPE (arg))) > > ret = 3; > > if (poly_int_tree_p (arg)) > > { > > poly_wide_int w = wi::to_poly_wide (arg); > > if (known_lt (w, 0)) > > return 4; > > else if (known_eq (w, 0)) > > return 2; > > else if (known_gt (w, 0)) > > return 1; > > else > > return 7; > > } > > value_range r; > > if (!get_range_query (cfun)->range_of_expr (r, arg) > > || r.undefined_p ()) > > return ret; > > if (r.nonpositive_p ()) > > ret &= ~1; > > if (r.nonzero_p ()) > > ret &= ~2; > > if (r.nonnegative_p ()) > > ret &= ~4; > > return ret; And the above should be short/simple enough to be added even if it just has a single user (ok, 2 in the same stmt). Could be even just a lambda if there are no other uses for it, so you would need to care less how to name it/where to declare etc. > > I doubt POLY_INT_CST will appear on what the function is being called on > > (types with scalar integral modes, mainly in .*_OVERFLOW expansion or say > > division/modulo expansion, but maybe my imagination is limited); > > so, if you think this is a good idea and the poly int in that case somehow > > guarantees the existing behavior (guess for signed it would be at least when > > not -fwrapv in action UB if the addition of the first POLY_INT_CST coeff > > and the others multiplied by the runtime value wraps around, but for > > unsigned is there a guarantee that if all the POLY_INT_CST coefficients > > don't have msb set that the resulting value will not have msb set either? > > I hope so, but ... Let's wait for Richard there. Anyway, if for the chrec case it only uses it on non-wrapping signed, then the POLY_INT_CST handling is fine in there... Jakub