From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 2B3783858CDB; Fri, 1 Dec 2023 08:27:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B3783858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701419268; bh=aGA99RVvOJ6EmgyvJ5nEcEInI5VfTPYfPUQLZz2H1ME=; h=From:To:Subject:Date:From; b=R/MIIgnCY/wEDxkzSUC9qlibEM9jZ7Io7FTlQkU6FFznLUT4/QEM1nfMCEPKUl6rR tPf7f6bMNwfeCaWVrZ6BI0i0tpH6W3H1lqsseXbLfV/BD/Caxs0jrhU2kQvLu4HvPE 05RfDlsRl0E4uMuvHVrX3yrkh3vAqFr93MgxH8Yc= 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 r14-6042] lower-bitint: Fix _BitInt .{ADD, SUB}_OVERFLOW lowering [PR112750] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 875c7771097d12f81f658f5f01ef52ec5b57f678 X-Git-Newrev: 364332658ef790d09d250db39c5b13e27c3543f1 Message-Id: <20231201082748.2B3783858CDB@sourceware.org> Date: Fri, 1 Dec 2023 08:27:48 +0000 (GMT) List-Id: https://gcc.gnu.org/g:364332658ef790d09d250db39c5b13e27c3543f1 commit r14-6042-g364332658ef790d09d250db39c5b13e27c3543f1 Author: Jakub Jelinek Date: Fri Dec 1 09:25:45 2023 +0100 lower-bitint: Fix _BitInt .{ADD,SUB}_OVERFLOW lowering [PR112750] The .{ADD,SUB}_OVERFLOW lowering is implemented by performing normal addition/subtraction (perhaps extended to even more bits than normally by continuing with extended values of operands after running of normal bits) and in addition to that trying to compute if certain sets of bits are either all zero or all sign extensions of the least significant bit. That code is in a lot of cases guarded just by a single condition (which can be idx > startlimb, idx >= startlimb or idx == startlimb) or by 2 conditions - if (idx >= startlimb) { if (idx == startlimb) ... else ... } Now, if_then_if_then_else when the second argument is NULL works just as if_then and sets m_gsi to be within the initially empty then block and that is where we emit code for constant tidx startlimb + (cmp_code == GT_EXPR). But in the 2 conditions case, m_gsi is set to the initially empty else block, so using EQ_EXPR for the condition was incorrect (and strangely nothing in the testsuite caught that), because the code for extracting the lowest set of bits (i.e. when tidx is startlimb) is then done when idx is not startlimb rather than when it is. The following patch fixes that. Note, when developing the lowering, I was using gcov to make sure all code is covered by the testsuite with minimum exceptions, so no idea how this slipped out. 2023-12-01 Jakub Jelinek PR middle-end/112750 * gimple-lower-bitint.cc (bitint_large_huge::lower_addsub_overflow): Use NE_EXPR rather than EQ_EXPR for g2 if !single_comparison and adjust probabilities. * gcc.dg/bitint-41.c: Use -std=c23 rather than -std=c2x. * gcc.dg/torture/bitint-43.c: Likewise. * gcc.dg/torture/bitint-44.c: Likewise. * gcc.dg/torture/bitint-45.c: New test. Diff: --- gcc/gimple-lower-bitint.cc | 4 ++-- gcc/testsuite/gcc.dg/bitint-41.c | 2 +- gcc/testsuite/gcc.dg/torture/bitint-43.c | 2 +- gcc/testsuite/gcc.dg/torture/bitint-44.c | 2 +- gcc/testsuite/gcc.dg/torture/bitint-45.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc index 5024815ed9d..09eb7a34e91 100644 --- a/gcc/gimple-lower-bitint.cc +++ b/gcc/gimple-lower-bitint.cc @@ -4028,11 +4028,11 @@ bitint_large_huge::lower_addsub_overflow (tree obj, gimple *stmt) edge edge_true_true, edge_true_false, edge_false; gimple *g2 = NULL; if (!single_comparison) - g2 = gimple_build_cond (EQ_EXPR, idx, + g2 = gimple_build_cond (NE_EXPR, idx, size_int (startlimb), NULL_TREE, NULL_TREE); if_then_if_then_else (g, g2, profile_probability::likely (), - profile_probability::unlikely (), + profile_probability::likely (), edge_true_true, edge_true_false, edge_false); unsigned tidx = startlimb + (cmp_code == GT_EXPR); diff --git a/gcc/testsuite/gcc.dg/bitint-41.c b/gcc/testsuite/gcc.dg/bitint-41.c index d87ea08bdf6..f97f03cd091 100644 --- a/gcc/testsuite/gcc.dg/bitint-41.c +++ b/gcc/testsuite/gcc.dg/bitint-41.c @@ -1,6 +1,6 @@ /* PR middle-end/112336 */ /* { dg-do compile { target bitint } } */ -/* { dg-options "-std=c2x" } */ +/* { dg-options "-std=c23" } */ unsigned _BitInt(1) v1; unsigned _BitInt(1) *p1 = &v1; diff --git a/gcc/testsuite/gcc.dg/torture/bitint-43.c b/gcc/testsuite/gcc.dg/torture/bitint-43.c index 4265bffef70..a45f5c6b493 100644 --- a/gcc/testsuite/gcc.dg/torture/bitint-43.c +++ b/gcc/testsuite/gcc.dg/torture/bitint-43.c @@ -1,6 +1,6 @@ /* PR c/111309 */ /* { dg-do run { target bitint } } */ -/* { dg-options "-std=c2x -pedantic-errors" } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ /* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ /* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ diff --git a/gcc/testsuite/gcc.dg/torture/bitint-44.c b/gcc/testsuite/gcc.dg/torture/bitint-44.c index 938c0e917f3..fe5c1685387 100644 --- a/gcc/testsuite/gcc.dg/torture/bitint-44.c +++ b/gcc/testsuite/gcc.dg/torture/bitint-44.c @@ -1,6 +1,6 @@ /* PR c/111309 */ /* { dg-do run { target bitint } } */ -/* { dg-options "-std=c2x -pedantic-errors" } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ /* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ /* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ diff --git a/gcc/testsuite/gcc.dg/torture/bitint-45.c b/gcc/testsuite/gcc.dg/torture/bitint-45.c new file mode 100644 index 00000000000..50c622d83d4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-45.c @@ -0,0 +1,32 @@ +/* PR middle-end/112750 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 256 +_BitInt(256) a = __INT_MAX__ + (_BitInt(256)) 1; +_BitInt(256) b = __INT_MAX__; +#endif +#if __BITINT_MAXWIDTH__ >= 512 +_BitInt(512) c = 0x7fffffffffffffffffffffffffffffffffffffffwb + (_BitInt(512)) 1; +_BitInt(512) d = 0x7fffffffffffffffffffffffffffffffffffffffwb; +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 256 + if (!__builtin_sub_overflow_p (a, 0, 0)) + __builtin_abort (); + if (!__builtin_sub_overflow_p (b, -1, 0)) + __builtin_abort (); +#endif +#if __BITINT_MAXWIDTH__ >= 512 + if (!__builtin_sub_overflow_p (c, 0, (_BitInt(160)) 0)) + __builtin_abort (); + if (!__builtin_sub_overflow_p (d, -1, 0)) + __builtin_abort (); +#endif + return 0; +}