https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108365

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I think the bug is in the C++ FE:
              /* When dividing two signed integers, we have to promote to int.
                 unless we divide by a constant != -1.  Note that default
                 conversion will have been performed on the operands at this
                 point, so we have to dig out the original type to find out if
                 it was unsigned.  */
              tree stripped_op1 = tree_strip_any_location_wrapper (op1);
              shorten = ((TREE_CODE (op0) == NOP_EXPR
                          && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
                         || (TREE_CODE (stripped_op1) == INTEGER_CST
                             && ! integer_all_onesp (stripped_op1)));
compare that to C FE, which does:
            /* Although it would be tempting to shorten always here, that
               loses on some targets, since the modulo instruction is
               undefined if the quotient can't be represented in the
               computation mode.  We shorten only if unsigned or if
               dividing by something we know != -1.  */
            shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
                       || (TREE_CODE (op1) == INTEGER_CST
                           && !integer_all_onesp (op1)));
C FE does that only if orig_op0 was unsigned, where orig_op0 is what is passed
to the function, where op0 is perhaps later promoted.  While the way it is
written in C++ FE
matches both unsigned {char,short} dividend promoted to int, but also the case
in the
testcase where orig_op0 is (long long) (unsigned long long) (-__INT_MAX__ - 1)
unfolded.  If op0 is promoted from unsigned type to wider signed type or if op0
has unsigned type, then shortening is of course possible, but if op0 is
converted from unsigned type to same sized signed type or to a narrower type,
we don't know if it can't be the signed minimum.

Reply via email to