https://gcc.gnu.org/g:4abe2b137f26173c489f4acb96f662ea542a0519

commit r17-6489-g4abe2b137f26173c489f4acb96f662ea542a0519
Author: Jakub Jelinek <[email protected]>
Date:   Fri Nov 20 10:17:30 2026 +0100

    bitintlower: Handle COMPLEX_CST lowering [PR128869]
    
    The following testcase is miscompiled during bitintlowering.
    We have
      # y_3 = PHI <y_7(3), 5(2), 0(4)>
      <L6>:
      _8 = .SUB_OVERFLOW 
(0x8000000000000000000000000000000000000000000000000000000000000000, y_3);
      _1 = IMAGPART_EXPR <_8>;
      _2 = (bool) _1;
      _9 = (int) _2;
      return _9;
    and the switch before this uses large/huge _BitInt, so we early in the
    pass lower the switch and do cleanup_tree_cfg.  In this case it removes
    some edges and y_3 is simplified into 0 and we end up with
      s_5 = (_BitInt(257)) c_4(D);
      _8 = __complex__ (0, 1);
      _1 = IMAGPART_EXPR <_8>;
      _2 = (bool) _1;
      _9 = (int) _2;
      return _9;
    as the content of the whole function.  Now, bitintlower doesn't handle
    COMPLEX_CST because it was never actually needed, it would be always
    propagated into the REALPART_EXPR and IMAGPART_EXPR uses thereof (note,
    we don't support _Complex _BitInt, only the uses of COMPLEX_TYPE of
    BITINT_TYPE in return values of .{ADD,SUB,MUL}_OVERFLOW and .CLMUL_WIDEN).
    But in this case such cleanup doesn't happen and for lower_stmt we actually
    even didn't call that at all (that one would ICE on unknown stmts), but
    we only handled BITINT_TYPE_P as SSA uses or sets and had special case
    for GIMPLE_CALL lhs.
    This patch fixes this by handling also COMPLEX_CST (by the same routine
    as COMPLEX_EXPR, only very small change is needed to find out the two
    halves) and tweaks the bitint kind discovery so that for all SSA_NAMEs
    it looks also for COMPLEX_TYPEs.
    
    2026-11-20  Jakub Jelinek  <[email protected]>
    
            PR tree-optimization/128869
            * gimple-lower-bitint.cc 
(bitint_large_huge::lower_complexexpr_stmt):
            Also handle COMPLEX_CST.
            (bitint_large_huge::lower_stmt): Likewise.
            (gimple_lower_bitint): Handle COMPLEX_TYPE of BITINT_TYPE_P as well.
    
            * gcc.dg/bitint-150.c: New test.
    
    Reviewed-by: Richard Biener <[email protected]>

Diff:
---
 gcc/gimple-lower-bitint.cc        | 20 +++++++++++++---
 gcc/testsuite/gcc.dg/bitint-150.c | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index 0958e82015c9..38188e697ed4 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -5543,14 +5543,20 @@ bitint_large_huge::lower_cplxpart_stmt (tree obj, 
gimple *stmt)
   insert_before (g);
 }
 
-/* Lower COMPLEX_EXPR stmt.  */
+/* Lower COMPLEX_EXPR or COMPLEX_CST stmt.  */
 
 void
 bitint_large_huge::lower_complexexpr_stmt (gimple *stmt)
 {
   tree lhs = gimple_assign_lhs (stmt);
-  tree rhs1 = gimple_assign_rhs1 (stmt);
-  tree rhs2 = gimple_assign_rhs2 (stmt);
+  tree rhs1 = gimple_assign_rhs1 (stmt), rhs2;
+  if (gimple_assign_rhs_code (stmt) == COMPLEX_EXPR)
+    rhs2 = gimple_assign_rhs2 (stmt);
+  else
+    {
+      rhs2 = TREE_IMAGPART (rhs1);
+      rhs1 = TREE_REALPART (rhs1);
+    }
   int part = var_to_partition (m_map, lhs);
   gcc_assert (m_vars[part] != NULL_TREE);
   lhs = m_vars[part];
@@ -7007,6 +7013,7 @@ bitint_large_huge::lower_stmt (gimple *stmt)
        lower_cplxpart_stmt (NULL_TREE, stmt);
        return;
       case COMPLEX_EXPR:
+      case COMPLEX_CST:
        lower_complexexpr_stmt (stmt);
        return;
       default:
@@ -8511,6 +8518,13 @@ gimple_lower_bitint (void)
                  = bitint_precision_kind (TREE_TYPE (t));
                kind = MAX (kind, this_kind);
              }
+           else if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
+                    && BITINT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
+             {
+               bitint_prec_kind this_kind
+                 = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t)));
+               kind = MAX (kind, this_kind);
+             }
          if (is_gimple_assign (stmt) && gimple_store_p (stmt))
            {
              t = gimple_assign_rhs1 (stmt);
diff --git a/gcc/testsuite/gcc.dg/bitint-150.c 
b/gcc/testsuite/gcc.dg/bitint-150.c
new file mode 100644
index 000000000000..4ea8b84fa2bb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/bitint-150.c
@@ -0,0 +1,48 @@
+/* PR tree-optimization/128869 */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-O1" } */
+
+[[gnu::noipa]] int
+foo (unsigned _BitInt(1) a, unsigned _BitInt(1) c)
+{
+#if __BITINT_MAXWIDTH__ >= 257
+  signed _BitInt(257) s = c, y;
+  switch (s)
+    {
+    case 14: y = a; break;
+    case 0x1e768b9a0533c4b55uwb: y = 5; break;
+    default: y = 0; break;
+    }
+  signed _BitInt(255) r;
+  /* 2^255 - 0 does not fit in signed _BitInt(255): must report overflow.  */
+  return __builtin_sub_overflow ((unsigned _BitInt(256)) 1 << 255, y, &r);
+#else
+  return 1;
+#endif
+}
+
+[[gnu::noipa]] int
+bar (unsigned _BitInt(1) a, unsigned _BitInt(1) c)
+{
+#if __BITINT_MAXWIDTH__ >= 257
+  signed _BitInt(257) s = c, y;
+  switch (s)
+    {
+    case 14: y = a; break;
+    case 0x1e768b9a0533c4b55uwb: y = 5; break;
+    default: y = 0; break;
+    }
+  signed _BitInt(127) r;
+  /* 2^127 - 0 does not fit in signed _BitInt(127): must report overflow.  */
+  return __builtin_sub_overflow ((unsigned _BitInt(128)) 1 << 127, y, &r);
+#else
+  return 1;
+#endif
+}
+
+int
+main ()
+{
+  if (foo (0, 0) != 1 || bar (0, 0) != 1)
+    __builtin_abort ();
+}

Reply via email to