https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128783
--- Comment #2 from Andrew Macleod <amacleod at redhat dot com> ---
Looks like its the w* operation. range calculations are:
210 range_of_expr(t_9) at stmt _2 = .MUL_OVERFLOW (t_9, t_9);
211 range_of_stmt (t_9) at stmt t_9 = (signed char) _3;
214 range_of_stmt (_3) at stmt _3 = _1 w* _12;
215 range_of_expr(_12) at stmt _3 = _1 w* _12;
218 range_of_expr(x_8(D)) at stmt _12 = (signed long) x_8(D);
TRUE : (218) range_of_expr (x_8(D)) [irange] int VARYING
TRUE : (215) range_of_expr (_12) [irange] signed long
[-2147483648, 2147483647]
221 range_of_expr(_1) at stmt _3 = _1 w* _12;
224 range_of_expr(b_7(D)) at stmt _1 = (unsigned long) b_7(D);
TRUE : (224) range_of_expr (b_7(D)) [irange] bool VARYING
TRUE : (221) range_of_expr (_1) [irange] unsigned long [0, 1]
MASK 0x1 VALUE 0x0
TRUE : (214) range_of_stmt (_3) [irange] uint128_t [0,
0][2147483647, 2147483647][340282366920938463463374607429620727808,
340282366920938463463374607429620727808]
TRUE : (211) range_of_stmt (t_9) [irange] signed char [-1, 0]
so _3 is calculated as [0,1] w* [-2147483648, 2147483647] , and is producing
uint128_t [0, 0][2147483647,
2147483647][340282366920938463463374607429620727808,
340282366920938463463374607429620727808]. Thats three singletons for which
the low bytes are 0x00 or 0xFF.. so the cast is correct [-1, 0] represents
that.
_3 is not calculated correctly, so it looks like a problem in calculating a
range for WIDEN_MULT_EXPR. op1 is unsigned (_1) and op2 is signed (_12).
Based on what the gimple_range_op handler does, it swaps the operands and
invokes operator_widen_mult_signed_unsigned..
void
operator_widen_mult_signed_unsigned::wi_fold (irange &r, tree type,
const wide_int &lh_lb,
const wide_int &lh_ub,
const wide_int &rh_lb,
const wide_int &rh_ub) const
{
wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED);
wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED);
wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED);
wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED);
/* We don't expect a widening multiplication to be able to overflow but range
calculations for multiplications are complicated. After widening the
operands lets call the base class. */
return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
}
The problem appears to be that casting both endpoints of an interval doesn’t
necessarily cast the interval itself.
The signed interval [-2147483648, 2147483647] crosses zero. After sign
extension, the lower endpoint still has the bit pattern for -2147483648. When
op_mult.wi_fold reads that pattern as unsigned, it sees 2^128 − 2147483648. The
upper endpoint still reads as 2147483647. So we are handing unsigned multiply a
single interval whose lower bound is above its upper bound.
I think the fix is to cast the whole range before multiplying which splits it
into two ordered unsigned intervals:
[0, 2147483647]
[2^128 − 2147483648, 2^128 − 1]
Then the normal multiply can fold each interval against the unsigned range [0,
1] and union the results.