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

commit r17-6121-g4d11a05988d3f57e947b705ed81247654a86004d
Author: Richard Biener <[email protected]>
Date:   Tue Nov 10 14:17:35 2026 +0100

    tree-optimization/128458 - do not drop pow calls without powi builtin
    
    When linearizing a multiplication chain, try_special_add_to_ops records
    a __builtin_pow call with a constant integer exponent as its base with
    an occurrence count.  attempt_builtin_powi then gives up when there is
    no __builtin_powi variant for the type of the chain, for example for
    _Float64 which is a distinct type from double, leaving the entry with
    the occurrence count in the operand vector.  The rest of reassoc treats
    that entry as a single occurrence of the base, so the pow call is
    dropped from the product.
    
    Instead of giving up, expand the powers as a series of multiplications
    as already done for integral types.
    
            PR tree-optimization/128458
            * tree-ssa-reassoc.cc (attempt_builtin_powi): Expand powers
            as multiplications if BUILT_IN_POWI does not exist for the
            type instead of giving up.
    
            * gcc.dg/pr128458.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/pr128458.c | 23 +++++++++++++++++++++++
 gcc/tree-ssa-reassoc.cc         | 13 +++++++------
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/pr128458.c b/gcc/testsuite/gcc.dg/pr128458.c
new file mode 100644
index 000000000000..342010c3673f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128458.c
@@ -0,0 +1,23 @@
+/* { dg-do run } */
+/* { dg-options "-Ofast" } */
+/* { dg-add-options float64 } */
+/* { dg-require-effective-target float64_runtime } */
+
+/* There is no __builtin_powi variant for _Float64 and reassoc dropped
+   the pow call from the product after linearizing it.  */
+
+extern double pow (double, double);
+
+__attribute__((noipa)) _Float64
+f (_Float64 x)
+{
+  return x * x * pow (x, 3.0);
+}
+
+int
+main (void)
+{
+  if (f (2) != 32)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc
index 63a37c38a4bf..22ffd06642eb 100644
--- a/gcc/tree-ssa-reassoc.cc
+++ b/gcc/tree-ssa-reassoc.cc
@@ -6519,10 +6519,11 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry 
*> *ops)
   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
   gimple *mul_stmt, *pow_stmt;
 
-  /* Nothing to do if BUILT_IN_POWI doesn't exist for this type and
-     target, unless type is integral.  */
-  if (!powi_fndecl && !INTEGRAL_TYPE_P (type))
-    return NULL_TREE;
+  /* Expand powers as a series of multiplications if type is integral
+     or BUILT_IN_POWI doesn't exist for this type and target.  We cannot
+     simply give up in the latter case as OPS can contain entries with
+     an occurrence count larger than one for __builtin_pow calls.  */
+  bool use_mults = INTEGRAL_TYPE_P (type) || !powi_fndecl;
 
   /* Allocate the repeated factor vector.  */
   repeat_factor_vec.create (10);
@@ -6630,7 +6631,7 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> 
*ops)
            }
          else
            {
-             if (INTEGRAL_TYPE_P (type))
+             if (use_mults)
                {
                  gcc_assert (power > 1);
                  gimple_stmt_iterator gsip = gsi;
@@ -6751,7 +6752,7 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> 
*ops)
          /* Form a call to __builtin_powi for the maximum product
             just formed, raised to the power obtained earlier.  */
          rf1 = &repeat_factor_vec[j];
-         if (INTEGRAL_TYPE_P (type))
+         if (use_mults)
            {
              gcc_assert (power > 1);
              gimple_stmt_iterator gsip = gsi;

Reply via email to