public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-469] Add MIN/MAX folding from fold_cond_expr_with_comparison to match.pd
Date: Mon, 16 May 2022 07:46:59 +0000 (GMT)	[thread overview]
Message-ID: <20220516074659.7B98F3858C56@sourceware.org> (raw)

https://gcc.gnu.org/g:9a53101caadae1b5c8d791d247b05268ee4f7f92

commit r13-469-g9a53101caadae1b5c8d791d247b05268ee4f7f92
Author: Richard Biener <rguenther@suse.de>
Date:   Fri May 13 09:35:30 2022 +0200

    Add MIN/MAX folding from fold_cond_expr_with_comparison to match.pd
    
    The following adds MIN/MAX folding from fold_cond_expr_with_comparison
    to the part GIMPLE of match.pd, leaving the GENERIC part in
    fold-const.cc since that's constrainted on frontend specific things
    I did not want to carry to match.pd.
    
    The effect becomes appearant when we no longer can rely on GENERIC
    folding of COND_EXPRs in gcc.dg/tree-ssa/pr92834.c and
    gcc.dg/tree-ssa/pr94786.c.
    
    2022-05-13  Richard Biener  <rguenther@suse.de>
    
            * match.pd (A cmp B ? A : B -> min/max): New patterns
            carried over from fold_cond_expr_with_comparison.

Diff:
---
 gcc/match.pd | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index f5efa77560c..0995055fcb5 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4450,6 +4450,52 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	(op (min @X { wide_int_to_tree (from_type, real_c1); })
 	    { wide_int_to_tree (from_type, c2); })))))))))
 
+#if GIMPLE
+/* A >= B ? A : B -> max (A, B) and friends.  The code is still
+   in fold_cond_expr_with_comparison for GENERIC folding with
+   some extra constraints.  */
+(for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
+ (simplify
+  (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
+        (convert3? @0) (convert4? @1))
+  (if (!HONOR_SIGNED_ZEROS (type)
+       && ((INTEGRAL_TYPE_P (type)
+	    /* Allow widening conversions of the data.  */
+	    && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
+	    && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
+	   || (tree_nop_conversion_p (type, TREE_TYPE (@0))
+	       && tree_nop_conversion_p (type, TREE_TYPE (@1)))))
+   (switch
+    (if (cmp == EQ_EXPR)
+     (if (VECTOR_TYPE_P (type))
+      (view_convert @c1)
+      (convert @c1)))
+    (if (cmp == NE_EXPR)
+     (if (VECTOR_TYPE_P (type))
+      (view_convert @c0)
+      (convert @c0)))
+    (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
+     (if (!HONOR_NANS (type))
+      (if (VECTOR_TYPE_P (type))
+       (view_convert (min @c0 @c1))
+       (convert (min @c0 @c1)))))
+    (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
+     (if (!HONOR_NANS (type))
+      (if (VECTOR_TYPE_P (type))
+       (view_convert (max @c0 @c1))
+       (convert (max @c0 @c1)))))
+    (if (cmp == UNEQ_EXPR)
+     (if (!HONOR_NANS (type))
+      (if (VECTOR_TYPE_P (type))
+       (view_convert @c1)
+       (convert @c1))))
+    (if (cmp == LTGT_EXPR)
+     (if (!HONOR_NANS (type))
+      (if (VECTOR_TYPE_P (type))
+       (view_convert @c0)
+       (convert @c0))))))))
+#endif
+
 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
 (simplify
  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)


                 reply	other threads:[~2022-05-16  7:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220516074659.7B98F3858C56@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@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).