]> git.ipfire.org Git - thirdparty/gcc.git/commit
match.pd: Fix up __builtin_mul_overflow_p signed type optimization [PR105984]
authorJakub Jelinek <jakub@redhat.com>
Thu, 16 Jun 2022 12:36:04 +0000 (14:36 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 16 Jun 2022 12:36:04 +0000 (14:36 +0200)
commit74e6a40335765077e235269f19d2d9905d0d9e44
treeff4bd2c8879911ed600cae6471b90129599d2c96
parent6a27c430468cb85454b19cef881a1422580657ff
match.pd: Fix up __builtin_mul_overflow_p signed type optimization [PR105984]

Earlier in the simplification pattern, we require that @0 has compatible
type to the type of IMAGPART_EXPR, but for @1 which is a non-zero constant
all we require is that it the constant fits into that type.
Later the code checks if the constant is negative, because when min / max
values are divided by negative divisor, lo will be higher than hi.
In the following testcase, @1 has unsigned char type, while @0 has
int type, so @1 which is 254 is wi::neg_p and we were swapping lo and hi,
even when @1 cast to int isn't negative.

We could use tree_int_cst_sgn (@1) < 0 as the check instead and it would
work both for narrower types of @1 and even same or wider ones, but
I've noticed we probably don't want to call fold_convert (TREE_TYPE (@0), @1)
twice and when we save that result in a temporary, we can just use wi::neg_p
on that temporary.

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

PR tree-optimization/105984
* match.pd (__builtin_mul_overflow_p (x, cst, (stype) 0) ->
x > stype_max / cst || x < stype_min / cst): fold_convert @1
to TREE_TYPE (@0) just once and test for negative divisor
also on that folded constant instead of on @1.

* gcc.c-torture/execute/pr105984.c: New test.
gcc/match.pd
gcc/testsuite/gcc.c-torture/execute/pr105984.c [new file with mode: 0644]