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.

Split out from earlier small series, re-bootstrap/regtest running
on x86_64-unknown-linux-gnu (sashiko also didn't pick it up).

Richard.

        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.
---
 gcc/testsuite/gcc.dg/pr128458.c | 23 +++++++++++++++++++++++
 gcc/tree-ssa-reassoc.cc         | 13 +++++++------
 2 files changed, 30 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr128458.c

diff --git a/gcc/testsuite/gcc.dg/pr128458.c b/gcc/testsuite/gcc.dg/pr128458.c
new file mode 100644
index 00000000000..342010c3673
--- /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 63a37c38a4b..22ffd06642e 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;

base-commit: 2a6eaf4a4676583f7e03f3e92aa16dd49ef65837
prerequisite-patch-id: e52d299db98b0892f974152cd9fa5f6859fec5b7
prerequisite-patch-id: 39da6ec3264bae454fa504f127cd5b80a0662f04
prerequisite-patch-id: 6d6c263a964425334ad8c2edd4dd89dcbff44bfe
prerequisite-patch-id: 9a12a1706cbdff1edf8959682cd3cf9d900cd8f1
prerequisite-patch-id: b24057443c46d6979436cce479836178b19355de
prerequisite-patch-id: d61bf2c4249ecd3d9da5eeda002d332ed3a8b4fd
prerequisite-patch-id: 59cbeca60f782f48f945bc532c2235a50de6f0e5
prerequisite-patch-id: 8eddd3172e2007d0fc591124a56b372dfe83e683
prerequisite-patch-id: 862842dd3742066d039f66ffe9528bc490308c50
prerequisite-patch-id: e413d39d9af45f25483eaf229cf883c771444fbb
prerequisite-patch-id: 2faef186ad1d816bb104d89638bc4cec6d863a46
prerequisite-patch-id: b32cceb111f4f57798e4b1d58822281e7bbaf25e
prerequisite-patch-id: 08ff1c06921c5e253d911128be3a8ebb76137d3c
prerequisite-patch-id: 72aa7ba47bd81909871a9e1a2e9de14f2cf42174
prerequisite-patch-id: 2ce10434bf0a7ab22e27ff177073e10b4b7d0d72
prerequisite-patch-id: e286b2e30887f3f43a3458c7c9b3f8c8b513a91b
prerequisite-patch-id: dc07b9957107f8b453a87ad64fd8477ae26e370c
prerequisite-patch-id: 146a9a34e1f46284793e3329e70d1d689efb4145
prerequisite-patch-id: 6f09ccfc22f0cc597e46f8bd2f77be989d4bc843
-- 
2.51.0

Reply via email to