On Thu, 1 Oct 2026, Jakub Jelinek wrote:

> On Thu, Oct 01, 2026 at 11:09:22AM +0200, Richard Biener wrote:
> > This fixes missed constant folding of .BITREVERSE (or other functions
> > with _BitInt return value) which was caused due to a SCALAR_INT_MODE_P
> > that fails on BLKmode _BitInt types.  Replace this with checking
> > the return type rather than its mode which isn't used anyway.
> > 
> > Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.
> > 
> > OK if that passes?
> 
> LGTM, though I'd change also
>   if (integer_cst_p (arg0) && integer_cst_p (arg1))
>     {
>       if (SCALAR_INT_MODE_P (mode))
> in the
> fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
> function for consistency.

Done and pushed as follows, bootstrapped/tested on 
x86_64-unknown-linux-gnu.

Richard.

>From a821925cdf5d3f87a1417231272f8f6fa844a43b Mon Sep 17 00:00:00 2001
From: Richard Biener <[email protected]>
Date: Thu, 1 Oct 2026 09:46:30 +0200
Subject: [PATCH] Fix missed constant folding of .BITREVERSE
To: [email protected]

This fixes missed constant folding of .BITREVERSE (or other functions
with _BitInt return value) which was caused due to a SCALAR_INT_MODE_P
that fails on BLKmode _BitInt types.  Replace this with checking
the return type rather than its mode which isn't used anyway.

        * fold-const-call.cc (fold_const_call_1): Check the
        return type rather than the mode for a scalar integral
        type before dispatching to fold_const_call_ss and
        fold_const_call_sss.

        * gcc.dg/tree-ssa/ssa-ccp-45.c: New testcase.
---
 gcc/fold-const-call.cc                     |  4 ++--
 gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-45.c | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-45.c

diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc
index 94841e8fb65..c04e93ce2ee 100644
--- a/gcc/fold-const-call.cc
+++ b/gcc/fold-const-call.cc
@@ -1311,7 +1311,7 @@ fold_const_call_1 (combined_fn fn, tree type, tree arg)
 
   if (integer_cst_p (arg))
     {
-      if (SCALAR_INT_MODE_P (mode))
+      if (INTEGRAL_TYPE_P (type))
        {
          wide_int result;
          if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
@@ -1762,7 +1762,7 @@ fold_const_call_1 (combined_fn fn, tree type, tree arg0, 
tree arg1)
 
   if (integer_cst_p (arg0) && integer_cst_p (arg1))
     {
-      if (SCALAR_INT_MODE_P (mode))
+      if (INTEGRAL_TYPE_P (type))
        {
          wide_int result;
          if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-45.c 
b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-45.c
new file mode 100644
index 00000000000..ea3c73350e7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-45.c
@@ -0,0 +1,16 @@
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-O -fdump-tree-ccp1" } */
+
+#if __BITINT_MAXWIDTH__ >= 129
+typedef unsigned _BitInt(129) T;
+
+T
+foo ()
+{
+  T y = (T) 1;
+  T x = __builtin_bitreverseg (y);
+  return x;
+}
+#endif
+
+/* { dg-final { scan-tree-dump-not "BITREVERSE" "ccp1" } } */
-- 
2.51.0

Reply via email to