https://gcc.gnu.org/g:a2d7bbb3494b789291d90d89e865b5ff063493e4
commit r17-6314-ga2d7bbb3494b789291d90d89e865b5ff063493e4 Author: Richard Biener <[email protected]> Date: Thu Nov 5 11:53:55 2026 +0100 tree-optimization/128453 - avoid truncating pow exponents in reassoc acceptable_pow_call accepts any integral pow exponent that fits a HOST_WIDE_INT, but the exponent is recorded in operand_entry::count, which is an unsigned int, so for example pow (x, 4294967296.0) gets a count of zero. attempt_builtin_powi then removes that entry from the operand list for free, dropping the pow call from the product entirely. Likewise the accumulated occurrence count of a factor is used as the int exponent of __builtin_powi without checking it fits. Widen operand_entry::count to HOST_WIDE_INT, reject exponents in acceptable_pow_call that do not fit the exponent argument type of __builtin_powi, avoid overflowing the accumulated occurrence counts and split powers exceeding the range of the __builtin_powi exponent argument over multiple calls. PR tree-optimization/128453 * tree-ssa-reassoc.h (operand_entry::count): Change type to HOST_WIDE_INT. * tree-ssa-reassoc.cc (acceptable_pow_call): Reject exponents not fitting integer_type_node. (attempt_builtin_powi): Record a factor in a separate element when accumulating its occurrence count would overflow. Limit the power of __builtin_powi calls to the maximum value of integer_type_node. * gcc.dg/pr128453-1.c: New testcase. * gcc.dg/pr128453-2.c: Likewise. Diff: --- gcc/testsuite/gcc.dg/pr128453-1.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/pr128453-2.c | 25 +++++++++++++++++++++++++ gcc/tree-ssa-reassoc.cc | 21 ++++++++++++++++----- gcc/tree-ssa-reassoc.h | 2 +- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/gcc/testsuite/gcc.dg/pr128453-1.c b/gcc/testsuite/gcc.dg/pr128453-1.c new file mode 100644 index 000000000000..8b98cdb84318 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr128453-1.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-options "-Ofast" } */ + +/* The pow exponent does not fit operand_entry::count and was + truncated to zero by reassoc, dropping the pow call. */ + +extern double pow (double, double); + +__attribute__((noipa)) double +f (double x, double y) +{ + return x * x * x * y * x * x * x * pow (x, 4294967296.0); +} + +int +main (void) +{ + if (f (-1.0, 1.0) != 1.0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr128453-2.c b/gcc/testsuite/gcc.dg/pr128453-2.c new file mode 100644 index 000000000000..d64c11348810 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr128453-2.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-Ofast -fdump-tree-reassoc1" } */ +/* { dg-require-effective-target int32 } */ + +/* The accumulated exponent does not fit the int argument of + __builtin_powi and has to be split over multiple calls. */ + +extern double pow (double, double); + +__attribute__((noipa)) double +f (double x) +{ + return x * x * pow (x, 2147483647.0); +} + +int +main (void) +{ + if (f (-1.0) != -1.0) + __builtin_abort (); + return 0; +} + +/* { dg-final { scan-tree-dump "__builtin_powi \\(x_\\d+\\(D\\), 2147483647\\)" "reassoc1" } } */ +/* { dg-final { scan-tree-dump "__builtin_powi \\(x_\\d+\\(D\\), 2\\)" "reassoc1" } } */ diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index 515c92e8c711..dda28b4204ad 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -6141,8 +6141,10 @@ acceptable_pow_call (gcall *stmt, tree *base, HOST_WIDE_INT *exponent) /* Expanding negative exponents is generally unproductive, so we don't complicate matters with those. Exponents of zero and one should - have been handled by expression folding. */ + have been handled by expression folding. Exponents not fitting + the exponent argument of __builtin_powi are not handled either. */ if (*exponent < 2 + || !wi::fits_to_tree_p (*exponent, integer_type_node) || TREE_CODE (*base) != SSA_NAME || !can_reassociate_op_p (*base)) return false; @@ -6526,7 +6528,10 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops) { FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1) { - if (rf1->factor == oe->op) + /* If the occurrence count would overflow, record the + factor in a separate element instead. */ + if (rf1->factor == oe->op + && oe->count <= HOST_WIDE_INT_MAX - rf1->count) { rf1->count += oe->count; break; @@ -6580,6 +6585,12 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops) vec_len = repeat_factor_vec.length (); + /* The occurrence counts can exceed the range of the exponent argument + of __builtin_powi, in which case multiple calls are emitted. */ + HOST_WIDE_INT max_power = HOST_WIDE_INT_MAX; + if (!use_mults) + max_power = tree_to_shwi (TYPE_MAX_VALUE (integer_type_node)); + /* Repeatedly look for opportunities to create a builtin_powi call. */ while (true) { @@ -6598,7 +6609,7 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops) if (j < vec_len) { - power = rf1->count; + power = MIN (rf1->count, max_power); if (power == 1) { @@ -6682,7 +6693,7 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops) if (j >= vec_len) break; - power = rf1->count; + power = MIN (rf1->count, max_power); if (dump_file && (dump_flags & TDF_DETAILS)) { @@ -6791,7 +6802,7 @@ attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops) factor from OPS. */ for (i = j; i < vec_len; i++) { - unsigned k = power; + HOST_WIDE_INT k = power; unsigned n; rf1 = &repeat_factor_vec[i]; diff --git a/gcc/tree-ssa-reassoc.h b/gcc/tree-ssa-reassoc.h index 2e16acd64485..0436f1a09325 100644 --- a/gcc/tree-ssa-reassoc.h +++ b/gcc/tree-ssa-reassoc.h @@ -26,7 +26,7 @@ struct operand_entry unsigned int rank; unsigned int id; tree op; - unsigned int count; + HOST_WIDE_INT count; gimple *stmt_to_insert; };
