https://gcc.gnu.org/g:57a50f4b58af716e325b7e630135a9732cf6b639
commit r17-6312-g57a50f4b58af716e325b7e630135a9732cf6b639 Author: Richard Biener <[email protected]> Date: Thu Nov 5 11:35:02 2026 +0100 Avoid repeated undistribution with negative pow exponents For (x * y) * __builtin_powi (x, 8000) + x * z * x * pow (x, -8000.0) with -Ofast reassoc1 generates thousands of multiplications. When counting candidate operands undistribute_ops_list linearizes the multiplication chains, expanding only __builtin_pow* calls accepted by acceptable_pow_call (positive exponents >= 2) into repeated operands, so x is counted once per chain. zero_one_operation however uses stmt_is_power_of_op which accepts any constant integer exponent, and removes x from the second chain by decrementing pow (x, -8000.0) to pow (x, -8001.0) instead of removing the explicit x that was counted. As the explicit factors remain, the rebuilt addition chain offers the same opportunity again and the process repeats until the __builtin_powi exponent is exhausted, emitting one multiplication per iteration. Fix this by making stmt_is_power_of_op agree with acceptable_pow_call so that only calls that linearization expands are treated as containing the operand. This also fixes wrong-code reported in PR128452. PR tree-optimization/128452 PR tree-optimization/128454 * tree-ssa-reassoc.cc (acceptable_pow_call): Forward declare. (stmt_is_power_of_op): Use acceptable_pow_call. * gcc.dg/tree-ssa/reassoc-56.c: New testcase. * gcc.dg/torture/pr128452.c: Likewise. Diff: --- gcc/testsuite/gcc.dg/torture/pr128452.c | 17 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/reassoc-56.c | 15 ++++++++++++ gcc/tree-ssa-reassoc.cc | 38 +++++++++++------------------- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr128452.c b/gcc/testsuite/gcc.dg/torture/pr128452.c new file mode 100644 index 000000000000..ca024695301e --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128452.c @@ -0,0 +1,17 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fassociative-math -fno-signed-zeros -fno-trapping-math" } */ + +__attribute__((noipa)) double +f (double x, double y, double z) +{ + return (x * y) * __builtin_pow (x, -9223372036854775808.0) + z * x; +} + +int main () +{ + if (f (-1.0, 1.0, 0.0) != -1.0) + __builtin_abort (); + if (f (-1.0, 1.0, 1.0) != -2.0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-56.c b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-56.c new file mode 100644 index 000000000000..79781ff671b2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-56.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-reassoc1" } */ + +double pow (double, double); + +double +f (double x, double y, double z) +{ + return (x * y) * __builtin_powi (x, 8000) + x * z * x * pow (x, -8000.0); +} + +/* Un-distribution of x must not strip x from pow (x, -8000.0) and + iterate until the __builtin_powi exponent is exhausted. */ +/* { dg-final { scan-tree-dump "pow \\(x_\[0-9\]+\\(D\\), -8\\.0e\\+3\\)" "reassoc1" } } */ +/* { dg-final { scan-tree-dump-times " \\* " 4 "reassoc1" } } */ diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index 22ffd06642eb..515c92e8c711 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -1189,38 +1189,28 @@ oecount_cmp (const void *p1, const void *p2) return c1->id > c2->id ? 1 : -1; } +static bool acceptable_pow_call (gcall *, tree *, HOST_WIDE_INT *); + /* Return TRUE iff STMT represents a builtin call that raises OP - to some constant integer exponent, as required by decrement_power. */ + to some constant integer exponent, as required by decrement_power. + This has to agree with the calls linearize_expr_tree expands into + repeated operands, otherwise zero_one_operation can strip OP from + a call that was not accounted for, for example by decrementing + a negative exponent over and over. */ static bool stmt_is_power_of_op (gimple *stmt, tree op) { + tree base; + HOST_WIDE_INT exponent; + if (!is_gimple_call (stmt)) return false; - switch (gimple_call_combined_fn (stmt)) - { - CASE_CFN_POW: - { - tree arg1 = gimple_call_arg (stmt, 1); - if (TREE_CODE (arg1) != REAL_CST) - return false; - REAL_VALUE_TYPE c = TREE_REAL_CST (arg1); - REAL_VALUE_TYPE cint; - real_from_integer (&cint, VOIDmode, real_to_integer (&c), SIGNED); - if (!real_identical (&c, &cint)) - return false; - return operand_equal_p (gimple_call_arg (stmt, 0), op, 0); - } - - CASE_CFN_POWI: - if (!tree_fits_shwi_p (gimple_call_arg (stmt, 1))) - return false; - return operand_equal_p (gimple_call_arg (stmt, 0), op, 0); - - default: - return false; - } + return (reassoc_insert_powi_p + && flag_unsafe_math_optimizations + && acceptable_pow_call (as_a <gcall *> (stmt), &base, &exponent) + && operand_equal_p (base, op, 0)); } /* Given STMT which is a __builtin_pow* call, decrement its exponent
