[Bug middle-end/30314] optimize multiply-by-constant overflow (wrap) test

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jun 3 09:42:55 GMT 2022


--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:


commit r13-979-g1982fe2692b6c3b7f969ffc4edac59f9d4359e91
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jun 3 11:41:21 2022 +0200

    match.pd: Optimize __builtin_mul_overflow_p (x, cst, (stype)0) [PR105777]

    The following patch is an incremental change to the PR30314 enhancement,
    this one handles signed types.
    For signed types (but still, the same for 1st and result element type
    and non-zero constant that fits into that type), we actually need to
    watch for overflow in direction to positive and negative infinity
    and it also depends on whether the cst operand is positive or negative.
    For __builtin_mul_overflow_p (x, cst, (stype) 0):
    For cst > 0, we can simplify it to:
    x > INT_MAX / cst || x < INT_MIN / cst
    x + (unsigned) (INT_MIN / cst) > (unsigned) (INT_MAX / cst) - (unsigned)
(INT_MIN / cst)
    and for cst < 0 to:
    x < INT_MAX / cst || x > INT_MIN / cst
    x + (unsigned) (INT_MAX / cst) > (unsigned) (INT_MIN / cst) - (unsigned)
(INT_MAX / cst)

    Additionally, I've added executable testcases, so we don't just check for
    the optimization to be performed, but also that it is correct (done that
    even for the other PR's testcase).

    2022-06-03  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/30314
            PR middle-end/105777
            * match.pd (__builtin_mul_overflow_p (x, cst, (stype) 0) ->
            x > stype_max / cst || x < stype_min / cst): New simplification.

            * gcc.dg/tree-ssa/pr30314.c: Add noipa attribute to all functions.
            * gcc.dg/tree-ssa/pr105777.c: New test.
            * gcc.c-torture/execute/pr30314.c: New test.
            * gcc.c-torture/execute/pr105777.c: New test.

More information about the Gcc-bugs mailing list