This is my proposed solution to PR123958 (and PR124002) which is a
regression exposed by my recent change to avoid expanding pow(x,2.0)
to x*x with -fmath-errno (the default) when we can't guarantee that
errno shouldn't be updated. The problem is that the logic to convert
pow(x,2.0) was also duplicated (but unused) in tree-ssa-math-opts
where it's intended to perform this conversion in order to expose
fused-multiply-add instructions when supported by the target. The
issue is that this "vestigial" code has bit-rotten over the years,
and incorrectly updates vdefs when changing vops, tiggering an ICE.
My pragmatic solution to this is to simply delete the problematic
code; the decision of whether pow(x,2.0) should be expanded is left
to the earlier pow_expand pass (which is what it's designed for), and
the later FMA pass can make use of any resulting FP multiplications.
Not only does this avoid the PHI related ICE, but also fixes the
original PR (on updating errno) on targets with FMA, e.g. aarch64
and recent x86_64 architectures (such as -march=znver3).
This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32},
with no new failures. Ok for mainline?
2026-02-06 Roger Sayle <[email protected]>
gcc/ChangeLog
PR middle-end/123826
PR tree-optimization/123958
PR c++/124002
* tree-ssa-math-opts.cc (math_opts_dom_walker::after_dom_children):
Delete code that (mis)handled conversion of pow(x,2.0) to x*x.
gcc/testsuite/ChangeLog
PR middle-end/123826
PR tree-optimization/123958
PR c++/124002
* g++.target/i386/pr124002.C: New test case.
* gcc.target/i386/pr123958.c: Likewise.
* gcc.dg/errno-4.c: Likewise.
Roger
--
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index 469de10a432..5947c8ab184 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -6597,25 +6597,6 @@ math_opts_dom_walker::after_dom_children (basic_block bb)
{
switch (gimple_call_combined_fn (stmt))
{
- CASE_CFN_POW:
- if (gimple_call_lhs (stmt)
- && TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
- && real_equal (&TREE_REAL_CST (gimple_call_arg (stmt, 1)),
- &dconst2)
- && convert_mult_to_fma (stmt,
- gimple_call_arg (stmt, 0),
- gimple_call_arg (stmt, 0),
- &fma_state))
- {
- unlink_stmt_vdef (stmt);
- if (gsi_remove (&gsi, true)
- && gimple_purge_dead_eh_edges (bb))
- *m_cfg_changed_p = true;
- release_defs (stmt);
- continue;
- }
- break;
-
case CFN_COND_MUL:
if (convert_mult_to_fma (stmt,
gimple_call_arg (stmt, 1),
diff --git a/gcc/testsuite/g++.target/i386/pr124002.C
b/gcc/testsuite/g++.target/i386/pr124002.C
new file mode 100644
index 00000000000..033cd3de870
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr124002.C
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=znver3" } */
+
+extern "C" double pow(double, double);
+double sigmoid_x;
+double sigmoid() {
+ if (sigmoid_x)
+ return sigmoid_x;
+ return 0;
+}
+double der_sigmoid() {
+ double tmp = sigmoid();
+ return tmp - pow(tmp, 2);
+}
+
diff --git a/gcc/testsuite/gcc.target/i386/pr123958.c
b/gcc/testsuite/gcc.target/i386/pr123958.c
new file mode 100644
index 00000000000..75064e880b4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr123958.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -march=znver3" } */
+
+float fnk00xX_rae1_1;
+double pow(double, double);
+double sqrt(double);
+void write_r4(float *);
+void fnk00xX() {
+ int i;
+ float rs2;
+ double ds2;
+ for (; i; i += 1) {
+ sqrt(i);
+ ds2 = ds2 + pow(fnk00xX_rae1_1, 2.0);
+ rs2 = ds2;
+ }
+ write_r4(&rs2);
+}
diff --git a/gcc/testsuite/gcc.dg/errno-4.c b/gcc/testsuite/gcc.dg/errno-4.c
new file mode 100644
index 00000000000..d7b80c1ca41
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/errno-4.c
@@ -0,0 +1,35 @@
+/* PR middle-end/123826 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#ifdef __NO_MATH_ERRNO__
+int main() { return 0; }
+#else
+#include <errno.h>
+#include <float.h>
+#include <math.h>
+
+double foo(double x)
+{
+ return x + pow(x, 2.0);
+}
+
+int main()
+{
+#ifdef math_errhandling
+#ifdef MATH_ERRNO
+ if ((math_errhandling & MATH_ERRNO) == 0)
+ return 0;
+#else
+ if ((math_errhandling & 1) == 0)
+ return 0;
+#endif
+#endif
+
+ errno = 0;
+ double x = foo(DBL_MAX);
+ if (errno != ERANGE)
+ __builtin_abort ();
+ return 0;
+}
+#endif