Now that we have parts of fold_binary_op_with_conditional_arg duplicated in match.pd and are using ! to take or throw away the result we have to be careful to not have both implementations play games which each other, causing quadratic behavior. In particular the match.pd implementation requires both arms to simplify while the fold-const.cc is happy with just one arm simplifying (something we cannot express in match.pd).
The fix is to simply not enable the match.pd pattern for GENERIC. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR middle-end/106617 * match.pd ((a ? b : c) > d -> a ? (b > d) : (c > d)): Fix guard, disable on GENERIC to not cause quadratic behavior with the fold-const.cc implementation and the use of ! * gcc.dg/pr106617.c: New testcase. --- gcc/match.pd | 4 +++- gcc/testsuite/gcc.dg/pr106617.c | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/pr106617.c diff --git a/gcc/match.pd b/gcc/match.pd index 07d0a61fc3a..1bb936fc401 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5796,6 +5796,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp (bit_and@2 @0 integer_pow2p@1) @1) (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) +#if GIMPLE /* From fold_binary_op_with_conditional_arg handle the case of rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the compares simplify. */ @@ -5805,8 +5806,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Do not move possibly trapping operations into the conditional as this pessimizes code and causes gimplification issues when applied late. */ (if (!FLOAT_TYPE_P (TREE_TYPE (@3)) - || operation_could_trap_p (cmp, true, false, @3)) + || !operation_could_trap_p (cmp, true, false, @3)) (cond @0 (cmp! @1 @3) (cmp! @2 @3))))) +#endif (for cmp (ge lt) /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */ diff --git a/gcc/testsuite/gcc.dg/pr106617.c b/gcc/testsuite/gcc.dg/pr106617.c new file mode 100644 index 00000000000..4274b55f80d --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106617.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ + +int nr_cpu_ids; +void fc_setup_exch_mgr() { + (((((((1UL << (((0, 0) + ? ((1) + ? (((nr_cpu_ids)) ? 0 + : ((nr_cpu_ids)) & (21) ? 21 + : ((nr_cpu_ids)) ? 20 + : ((nr_cpu_ids)) & (19) ? 19 + : ((nr_cpu_ids)) ? 18 + : ((nr_cpu_ids)) & (17) ? 17 + : ((nr_cpu_ids)) ? 16 + : ((nr_cpu_ids)) & (15) ? 15 + : ((nr_cpu_ids)) ? 14 + : ((nr_cpu_ids)) & (13) ? 13 + : ((nr_cpu_ids)) ? 12 + : ((nr_cpu_ids)) & (11) ? 11 + : ((nr_cpu_ids)) ? 10 + : ((nr_cpu_ids)) & (9) ? 9 + : ((nr_cpu_ids)) ? 8 + : ((nr_cpu_ids)) & (7) ? 7 + : ((nr_cpu_ids)) ? 6 + : ((nr_cpu_ids)) & (5) ? 5 + : ((nr_cpu_ids)) ? 4 + : ((nr_cpu_ids)) & (3) + ? 3 + : ((nr_cpu_ids)-1) & 1) + : 1) + : 0) + + 1))))) & + (1UL << 2) + ? 2 + : 1)) + ); +} -- 2.35.3