From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 4066A3851C21; Thu, 2 Sep 2021 09:26:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4066A3851C21 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-3312] match.pd: Demote IFN_{ADD, SUB, MUL}_OVERFLOW operands [PR99591] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 1e6267b335262eb6244c86a7102f00b26e57af4d X-Git-Newrev: 2af6dd77ea742d4ee911f466878624972929508a Message-Id: <20210902092630.4066A3851C21@sourceware.org> Date: Thu, 2 Sep 2021 09:26:30 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Sep 2021 09:26:30 -0000 https://gcc.gnu.org/g:2af6dd77ea742d4ee911f466878624972929508a commit r12-3312-g2af6dd77ea742d4ee911f466878624972929508a Author: Jakub Jelinek Date: Thu Sep 2 11:25:07 2021 +0200 match.pd: Demote IFN_{ADD,SUB,MUL}_OVERFLOW operands [PR99591] The overflow builtins work on infinite precision integers and then convert to the result type's precision, so any argument promotions are useless. The expand_arith_overflow expansion is able to demote the arguments itself through get_range_pos_neg and get_min_precision calls and if needed promote to whatever mode it decides to perform the operations in, but if there are any promotions it demoted, those are already expanded. Normally combine would remove the useless sign or zero extensions when it sees the result of those is only used in a lowpart subreg, but typically those lowpart subregs appear multiple times in the pattern so that they describe properly the overflow behavior and combine gives up, so we end up with e.g. movswl %si, %esi movswl %di, %edi imulw %si, %di seto %al where both movswl insns are useless. The following patch fixes it by demoting operands of the ifns (only gets rid of integral to integral conversions that increase precision). While IFN_{ADD,MUL}_OVERFLOW are commutative and just one simplify would be enough, IFN_SUB_OVERFLOW is not, therefore two simplifications. 2021-09-02 Jakub Jelinek PR tree-optimization/99591 * match.pd: Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW if they were promoted. * gcc.target/i386/pr99591.c: New test. * gcc.target/i386/pr97950.c: Match or reject setb or jn?b instructions together with seta or jn?a. Diff: --- gcc/match.pd | 15 +++++++++++++++ gcc/testsuite/gcc.target/i386/pr97950.c | 4 ++-- gcc/testsuite/gcc.target/i386/pr99591.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index f421c74b62c..f920bc4b7c1 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5587,6 +5587,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) +/* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */ +(for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW) + (simplify + (ovf (convert@2 @0) @1) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@2)) + && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))) + (ovf @0 @1))) + (simplify + (ovf @1 (convert@2 @0)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@2)) + && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))) + (ovf @1 @0)))) + /* Simplification of math builtins. These rules must all be optimizations as well as IL simplifications. If there is a possibility that the new form could be a pessimization, the rule should go in the canonicalization diff --git a/gcc/testsuite/gcc.target/i386/pr97950.c b/gcc/testsuite/gcc.target/i386/pr97950.c index 277311d9a4e..185d73378cb 100644 --- a/gcc/testsuite/gcc.target/i386/pr97950.c +++ b/gcc/testsuite/gcc.target/i386/pr97950.c @@ -1,10 +1,10 @@ /* PR target/95950 */ /* { dg-do compile } */ /* { dg-options "-O2 -mtune=generic" } */ -/* { dg-final { scan-assembler-times "\tseta\t" 4 } } */ +/* { dg-final { scan-assembler-times "\tset\[ab]\t" 4 } } */ /* { dg-final { scan-assembler-times "\tseto\t" 16 } } */ /* { dg-final { scan-assembler-times "\tsetc\t" 4 } } */ -/* { dg-final { scan-assembler-not "\tjn?a\t" } } */ +/* { dg-final { scan-assembler-not "\tjn?\[ab]\t" } } */ /* { dg-final { scan-assembler-not "\tjn?o\t" } } */ /* { dg-final { scan-assembler-not "\tjn?c\t" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr99591.c b/gcc/testsuite/gcc.target/i386/pr99591.c new file mode 100644 index 00000000000..01d84858401 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr99591.c @@ -0,0 +1,32 @@ +/* PR tree-optimization/99591 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ +/* { dg-final { scan-assembler-not "\tmovs\[bw]l\t" } } */ + +int +foo (signed char a, signed char b) +{ + signed char r; + return __builtin_add_overflow (a, b, &r); +} + +int +bar (short a, short b) +{ + short r; + return __builtin_add_overflow (a, b, &r); +} + +int +baz (signed char a, signed char b) +{ + signed char r; + return __builtin_add_overflow ((int) a, (int) b, &r); +} + +int +qux (short a, short b) +{ + short r; + return __builtin_add_overflow ((int) a, (int) b, &r); +}