public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-115352-4-mGBusSh8kc@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-115352-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115352

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
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 = gimple_build_assign (this_ovf, NE_EXPR, l, cmp);
                  insert_before (g);
-                 if (cmp_code == GT_EXPR)
+                 if (cmp_code == GT_EXPR
+                     || (cmp_code == GE_EXPR && single_comparison))
                    {
                      tree t = make_ssa_name (boolean_type_node);
                      g = 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))
0x00000000000000000c377e8a3fd1881fff035bb487a51c9ed1f7350befa7ec4450000000000000000a3cf8d1ebb723981wb))
    __builtin_abort ();
  if (!foo
(-0x1ffffffffffffffffc377e8a3fd1881fff035bb487a51c9ed1f7350befa7ec445ffffffffffffffffa3cf8d1ebb723981uwb))
    __builtin_abort ();
  if (!foo (-(_BitInt (385))
0x00000000000000000ffffffffffffffffffffffffffffffff00000000000000000000000000000000a3cf8d1ebb723981wb))
    __builtin_abort ();
  if (!foo
(-0x1ffffffffffffffff00000000000000000000000000000000ffffffffffffffffffffffffffffffffa3cf8d1ebb723981uwb))
    __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
                    = (startlimb + 2 >= fin || (startlimb & 1) != (i & 1));
                  if (!single_comparison)
-                   {
-                     cmp_code = GE_EXPR;
-                     if (!check_zero && (start % limb_prec) == 0)
-                       single_comparison = true;
-                   }
+                   cmp_code = GE_EXPR;
                  else if ((startlimb & 1) == (i & 1))
                    cmp_code = EQ_EXPR;
                  else
The idea behind the optimization was that arith_overflow_extract_bits in those
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 completely
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.

  parent reply	other threads:[~2024-06-06 10:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-05  3:39 [Bug tree-optimization/115352] New: " zsojka at seznam dot cz
2024-06-05  9:30 ` [Bug middle-end/115352] " pinskia at gcc dot gnu.org
2024-06-06 10:17 ` jakub at gcc dot gnu.org [this message]
2024-06-06 11:06 ` jakub at gcc dot gnu.org
2024-06-07  8:33 ` cvs-commit at gcc dot gnu.org
2024-06-07  8:35 ` cvs-commit at gcc dot gnu.org
2024-06-07  8:42 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-115352-4-mGBusSh8kc@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).