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  <ja...@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 via email to