From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id AA2BA3858D37; Thu, 22 Feb 2024 09:14:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AA2BA3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708593291; bh=aGwDHIV/Z6cHi8VTs/GRhpjdK0ZKZPk2gnMklXA9VZc=; h=From:To:Subject:Date:From; b=Bs9GlBd88Q+y7u+5h0sD5z4KwYATBqO8Tqz/zL//p8HOdDCnWNnWKOndwf7HFTupC FfKrMvxkYCBN2U4jydp9WCNB8Vxehn6IFOQ8rlwl0SZXv/nGwyW4imJZwSOpkMc0Ik QrVO15D9Vk/VylHerfiIUF/dvi83DtQnpjy2Y2Q0= 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-9131] bitintlower: Fix .MUL_OVERFLOW overflow checking [PR114038] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 00bc8c0998d83fd08c567607f2e052108bfa39c7 X-Git-Newrev: 853cbcb7a74ecd95efcba25279b270f0a868d584 Message-Id: <20240222091451.AA2BA3858D37@sourceware.org> Date: Thu, 22 Feb 2024 09:14:51 +0000 (GMT) List-Id: https://gcc.gnu.org/g:853cbcb7a74ecd95efcba25279b270f0a868d584 commit r14-9131-g853cbcb7a74ecd95efcba25279b270f0a868d584 Author: Jakub Jelinek Date: Thu Feb 22 10:14:00 2024 +0100 bitintlower: Fix .MUL_OVERFLOW overflow checking [PR114038] Currently, bitint_large_huge::lower_mul_overflow uses cnt 1 only if startlimb == endlimb and in that case doesn't use a loop and handles everything in a special if: unsigned cnt; bool use_loop = false; if (startlimb == endlimb) cnt = 1; else if (startlimb + 1 == endlimb) cnt = 2; else if ((end % limb_prec) == 0) { cnt = 2; use_loop = true; } else { cnt = 3; use_loop = startlimb + 2 < endlimb; } if (cnt == 1) { ... } else The loop handling for the loop exit condition wants to compare if the incremented index is equal to endlimb, but that is correct only if end is not divisible by limb_prec and there will be a straight line check after the loop as well for the most significant limb. The code used endlimb + (cnt == 1) for that, but cnt == 1 is never true here, because cnt is either 2 or 3, so the right check is (cnt == 2). 2024-02-22 Jakub Jelinek PR tree-optimization/114038 * gimple-lower-bitint.cc (bitint_large_huge::lower_mul_overflow): Fix loop exit condition if end is divisible by limb_prec. * gcc.dg/torture/bitint-59.c: New test. Diff: --- gcc/gimple-lower-bitint.cc | 2 +- gcc/testsuite/gcc.dg/torture/bitint-59.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc index 13b9b205df79..fb03063f86e4 100644 --- a/gcc/gimple-lower-bitint.cc +++ b/gcc/gimple-lower-bitint.cc @@ -4497,7 +4497,7 @@ bitint_large_huge::lower_mul_overflow (tree obj, gimple *stmt) size_one_node); insert_before (g); g = gimple_build_cond (NE_EXPR, idx_next, - size_int (endlimb + (cnt == 1)), + size_int (endlimb + (cnt == 2)), NULL_TREE, NULL_TREE); insert_before (g); edge true_edge, false_edge; diff --git a/gcc/testsuite/gcc.dg/torture/bitint-59.c b/gcc/testsuite/gcc.dg/torture/bitint-59.c new file mode 100644 index 000000000000..ef17424dfa85 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-59.c @@ -0,0 +1,22 @@ +/* PR tree-optimization/114038 */ +/* { 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__ >= 129 +int +foo (unsigned _BitInt(63) x, unsigned _BitInt(129) y) +{ + return __builtin_mul_overflow_p (y, x, 0); +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 129 + if (!foo (90, 0x80000000000000000000000000000000uwb)) + __builtin_abort (); +#endif +}