Fix missed constant folding of .BITREVERSE

Message ID r901qp50-s266-09or-092q-6874qs26pnqq@fhfr.qr
State New
Headers
Series Fix missed constant folding of .BITREVERSE |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply

Commit Message

Richard Biener Oct. 1, 2026, 9:09 a.m. UTC
  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?

I didn't quite understand where you wanted extra checks - given
that 'arg' is integer_cst_p its mode will surely be scalar integer
if it isn't a BIT_INT_TYPE(?).

Thanks,
Richard.

	* 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.

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

Comments

Jakub Jelinek Oct. 1, 2026, 9:13 a.m. UTC | #1
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.

	Jakub
  
Richard Biener Oct. 1, 2026, 11:39 a.m. UTC | #2
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 <rguenther@suse.de>
Date: Thu, 1 Oct 2026 09:46:30 +0200
Subject: [PATCH] Fix missed constant folding of .BITREVERSE
To: gcc-patches@gcc.gnu.org

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" } } */
  

Patch

diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc
index 94841e8fb65..2a8316bf406 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),
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" } } */