From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7BA4E38E53F0; Thu, 6 Jun 2024 10:17:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7BA4E38E53F0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1717669066; bh=Jg2bQpyQ+zP8gOHEBsPSLKHCfdnXlNqb+W++kTlefXI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VkhkmNB60kDZzAG3WCdS/9SPcnfr/GFMCcCSDA/2x3Le1ezySDKWM/QdkGXthCzIk +mtHUv0bd5gb5y7Vj0f8OWKG6yCNB9eexHDLDS8i0xpcrNwAWhtnUHijgX29kTzznE wrKzVmxPCDoxvFOLRtVOEGjCU38+vFeHTgyHtw/Y= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/115352] wrong code with _BitInt() __builtin_sub_overflow_p() at -O0 Date: Thu, 06 Jun 2024 10:17:46 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 15.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D115352 --- Comment #2 from Jakub Jelinek --- I've first tried --- gcc/gimple-lower-bitint.cc.jj 2024-04-12 10:59:48.233153262 +0200 +++ gcc/gimple-lower-bitint.cc 2024-06-06 11:05:29.845597763 +0200 @@ -4324,7 +4324,8 @@ bitint_large_huge::lower_addsub_overflow else g =3D gimple_build_assign (this_ovf, NE_EXPR, l, cmp); insert_before (g); - if (cmp_code =3D=3D GT_EXPR) + if (cmp_code =3D=3D GT_EXPR + || (cmp_code =3D=3D GE_EXPR && single_comparison)) { tree t =3D make_ssa_name (boolean_type_node); g =3D gimple_build_assign (t, BIT_IOR_EXPR, ovf, this= _ovf); which fixes the #c0 testcase. But on the reduced int foo (_BitInt (385) b) { return __builtin_sub_overflow_p (0, b, (_BitInt (65)) 0); } int main () { if (!foo (-(_BitInt (385)) 0x00000000000000000c377e8a3fd1881fff035bb487a51c9ed1f7350befa7ec44500000000= 00000000a3cf8d1ebb723981wb)) __builtin_abort (); if (!foo (-0x1ffffffffffffffffc377e8a3fd1881fff035bb487a51c9ed1f7350befa7ec445ffffff= ffffffffffa3cf8d1ebb723981uwb)) __builtin_abort (); if (!foo (-(_BitInt (385)) 0x00000000000000000ffffffffffffffffffffffffffffffff000000000000000000000000= 00000000a3cf8d1ebb723981wb)) __builtin_abort (); if (!foo (-0x1ffffffffffffffff00000000000000000000000000000000ffffffffffffffffffffff= ffffffffffa3cf8d1ebb723981uwb)) __builtin_abort (); } testcase it only fixes the first 2 calls but not the last 2. So, I'm afraid I just need to kill the optimization instead: --- gcc/gimple-lower-bitint.cc.jj 2024-04-12 10:59:48.233153262 +0200 +++ gcc/gimple-lower-bitint.cc 2024-06-06 12:06:57.065717651 +0200 @@ -4286,11 +4286,7 @@ bitint_large_huge::lower_addsub_overflow bool single_comparison =3D (startlimb + 2 >=3D fin || (startlimb & 1) !=3D (i = & 1)); if (!single_comparison) - { - cmp_code =3D GE_EXPR; - if (!check_zero && (start % limb_prec) =3D=3D 0) - single_comparison =3D true; - } + cmp_code =3D GE_EXPR; else if ((startlimb & 1) =3D=3D (i & 1)) cmp_code =3D EQ_EXPR; else The idea behind the optimization was that arith_overflow_extract_bits in th= ose cases is the same, we just extract all bits from the limb, whether it is the limb at the boundary (i.e. EQ_EXPR to the compared limb index) or above (GT_EXPR), so GE_EXPR would do. Except without the first patch it complete= ly ignored the previously accumulated mismatches (i.e. overflows) from lower limbs, and while that no longer is the case with the first patch, it still ignores whether the upper bits were all 0s or all 1s previously and as long= as they are again all 0s or all 1s, it happily makes it non-overflow case and = what the next limb should compare against.=