https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125025
--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
So step is -128 and
if (integer_nonzerop (iv0->step))
step = fold_convert (niter_type, iv0->step);
else
step = fold_convert (niter_type,
fold_build1 (NEGATE_EXPR, type, iv1->step));
produces (unsigned char) - (-128) since we refuse to constant fold - -128.
But I think SCEV starts out wrong here already, analyzing
<bb 3> :
# a0_14 = PHI <30(2), a0_22(5)>
lbl_br10:
if (a0_14 <= 0)
goto <bb 4>; [INV]
else
goto <bb 5>; [INV]
<bb 5> :
a0.3_11 = (unsigned char) a0_14;
_13 = a0.3_11 + 128;
a0_22 = (signed char) _13;
// predicted unlikely by goto predictor.
goto <bb 3>; [100.00%]
a0_14 as {30, +, -128}_1
that might be OK for the particular loop, but it does look worth investigating.
That said, we know that iv1 is negative due to
/* {base, -C} < n, or n < {base, C} */
if (tree_int_cst_sign_bit (iv0->step)
|| (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step)))
return number_of_iterations_until_wrap (loop, type, iv0, iv1, niter);
so instead of
step = fold_convert (niter_type,
fold_build1 (NEGATE_EXPR, type, iv1->step));
we can use ABSU_EXPR or alternatively first convert to niter_type (unsigned)
and then negate. We're doing the latter in one place already, there's
one other obvious place to fix.
So I'm testing that solution.