]> gcc.gnu.org Git - gcc.git/commit
match.pd: Optimize __builtin_mul_overflow_p (x, cst, (stype)0) [PR105777]
authorJakub Jelinek <jakub@redhat.com>
Fri, 3 Jun 2022 09:41:21 +0000 (11:41 +0200)
committerJakub Jelinek <jakub@redhat.com>
Fri, 3 Jun 2022 09:42:35 +0000 (11:42 +0200)
commit1982fe2692b6c3b7f969ffc4edac59f9d4359e91
tree8f2ee8226b46aa7916a51f8748927ad3f2395ac1
parent0fd3c7063db52b8f509a9572ff8078a0f91176da
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
aka:
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
aka:
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.
gcc/match.pd
gcc/testsuite/gcc.c-torture/execute/pr105777.c [new file with mode: 0644]
gcc/testsuite/gcc.c-torture/execute/pr30314.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr105777.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr30314.c
This page took 0.069996 seconds and 5 git commands to generate.