public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] lower-bitint: Fix .{ADD,SUB,MUL}_OVERFLOW with _BitInt large/huge INTEGER_CST arguments [PR113003]
Date: Thu, 14 Dec 2023 23:14:28 +0100	[thread overview]
Message-ID: <ZXt+RP0GTiKhocIW@tucnak> (raw)

Hi!

As shown in the testcase, .{ADD,SUB,MUL}_OVERFLOW calls are another
exception to the middle/large/huge _BitInt discovery through SSA_NAMEs
next to stores of INTEGER_CSTs to memory and their conversions to
floating point.
The calls can have normal COMPLEX_TYPE with INTEGER_TYPE elts return type
(or BITINT_TYPE with small precision) and one of the arguments can be
SSA_NAME with an INTEGER_TYPE or small BITINT_TYPE as well; still, when
there is an INTEGER_CST argument with large/huge BITINT_TYPE, we need to
lower it that way.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2023-12-14  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/113003
	* gimple-lower-bitint.cc (arith_overflow_arg_kind): New function.
	(gimple_lower_bitint): Use it to catch .{ADD,SUB,MUL}_OVERFLOW
	calls with large/huge INTEGER_CST arguments.

	* gcc.dg/bitint-54.c: New test.

--- gcc/gimple-lower-bitint.cc.jj	2023-12-13 11:36:21.636633517 +0100
+++ gcc/gimple-lower-bitint.cc	2023-12-14 13:28:17.660737468 +0100
@@ -5641,6 +5641,37 @@ build_bitint_stmt_ssa_conflicts (gimple
     def (live, lhs, graph);
 }
 
+/* If STMT is .{ADD,SUB,MUL}_OVERFLOW with INTEGER_CST arguments,
+   return the largest bitint_prec_kind of them, otherwise return
+   bitint_prec_small.  */
+
+static bitint_prec_kind
+arith_overflow_arg_kind (gimple *stmt)
+{
+  bitint_prec_kind ret = bitint_prec_small;
+  if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
+    switch (gimple_call_internal_fn (stmt))
+      {
+      case IFN_ADD_OVERFLOW:
+      case IFN_SUB_OVERFLOW:
+      case IFN_MUL_OVERFLOW:
+	for (int i = 0; i < 2; ++i)
+	  {
+	    tree a = gimple_call_arg (stmt, i);
+	    if (TREE_CODE (a) == INTEGER_CST
+		&& TREE_CODE (TREE_TYPE (a)) == BITINT_TYPE)
+	      {
+		bitint_prec_kind kind = bitint_precision_kind (TREE_TYPE (a));
+		ret = MAX (ret, kind);
+	      }
+	  }
+	break;
+      default:
+	break;
+      }
+  return ret;
+}
+
 /* Entry point for _BitInt(N) operation lowering during optimization.  */
 
 static unsigned int
@@ -5657,7 +5688,12 @@ gimple_lower_bitint (void)
 	continue;
       tree type = TREE_TYPE (s);
       if (TREE_CODE (type) == COMPLEX_TYPE)
-	type = TREE_TYPE (type);
+	{
+	  if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s))
+	      != bitint_prec_small)
+	    break;
+	  type = TREE_TYPE (type);
+	}
       if (TREE_CODE (type) == BITINT_TYPE
 	  && bitint_precision_kind (type) != bitint_prec_small)
 	break;
@@ -5745,7 +5781,12 @@ gimple_lower_bitint (void)
 	continue;
       tree type = TREE_TYPE (s);
       if (TREE_CODE (type) == COMPLEX_TYPE)
-	type = TREE_TYPE (type);
+	{
+	  if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s))
+	      >= bitint_prec_large)
+	    has_large_huge = true;
+	  type = TREE_TYPE (type);
+	}
       if (TREE_CODE (type) == BITINT_TYPE
 	  && bitint_precision_kind (type) >= bitint_prec_large)
 	{
@@ -6245,8 +6286,7 @@ gimple_lower_bitint (void)
 	      {
 		bitint_prec_kind this_kind
 		  = bitint_precision_kind (TREE_TYPE (t));
-		if (this_kind > kind)
-		  kind = this_kind;
+		kind = MAX (kind, this_kind);
 	      }
 	  if (is_gimple_assign (stmt) && gimple_store_p (stmt))
 	    {
@@ -6255,8 +6295,7 @@ gimple_lower_bitint (void)
 		{
 		  bitint_prec_kind this_kind
 		    = bitint_precision_kind (TREE_TYPE (t));
-		  if (this_kind > kind)
-		    kind = this_kind;
+		  kind = MAX (kind, this_kind);
 		}
 	    }
 	  if (is_gimple_assign (stmt)
@@ -6268,21 +6307,22 @@ gimple_lower_bitint (void)
 		{
 		  bitint_prec_kind this_kind
 		    = bitint_precision_kind (TREE_TYPE (t));
-		  if (this_kind > kind)
-		    kind = this_kind;
+		  kind = MAX (kind, this_kind);
 		}
 	    }
 	  if (is_gimple_call (stmt))
 	    {
 	      t = gimple_call_lhs (stmt);
-	      if (t
-		  && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
-		  && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE)
+	      if (t && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
 		{
-		  bitint_prec_kind this_kind
-		    = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t)));
-		  if (this_kind > kind)
-		    kind = this_kind;
+		  bitint_prec_kind this_kind = arith_overflow_arg_kind (stmt);
+		  kind = MAX (kind, this_kind);
+		  if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE)
+		    {
+		      this_kind
+			= bitint_precision_kind (TREE_TYPE (TREE_TYPE (t)));
+		      kind = MAX (kind, this_kind);
+		    }
 		}
 	    }
 	  if (kind == bitint_prec_small)
--- gcc/testsuite/gcc.dg/bitint-54.c.jj	2023-12-14 13:47:46.683512224 +0100
+++ gcc/testsuite/gcc.dg/bitint-54.c	2023-12-14 13:47:20.191879500 +0100
@@ -0,0 +1,21 @@
+/* PR tree-optimization/113003 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c23 -O2" } */
+
+#if __BITINT_MAXWIDTH__ >= 131
+int
+foo (_BitInt(7) x)
+{
+  return __builtin_mul_overflow_p (x, 1046555807606105294475452482332716433408wb, 0);
+}
+
+#ifdef __SIZEOF_INT128__
+int
+bar (unsigned __int128 x)
+{
+  return __builtin_sub_overflow_p (340282366920938463463374607431768211457uwb, x, 0);
+}
+#endif
+#else
+int i;
+#endif

	Jakub


             reply	other threads:[~2023-12-14 22:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 22:14 Jakub Jelinek [this message]
2023-12-15  7:39 ` Richard Biener

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=ZXt+RP0GTiKhocIW@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /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).