public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] middle-end/106617 - fix fold_binary_op_with_conditional_arg pattern issue
Date: Thu, 18 Aug 2022 14:03:27 +0000 (UTC)	[thread overview]
Message-ID: <20220818140327.s5oJJICnNKvwgftj6fdI3fW_LfGbX9VhGib8Yt_umlA@z> (raw)

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

             reply	other threads:[~2022-08-18 14:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-18 14:03 Richard Biener [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-08-18 14:03 Richard Biener
2022-08-18 14:03 Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220818140327.s5oJJICnNKvwgftj6fdI3fW_LfGbX9VhGib8Yt_umlA@z \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).