From: Andrew Pinski <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH 6/7] MATCH: Factor out code that for min max detection with constants
Date: Mon, 24 Apr 2023 14:30:10 -0700 [thread overview]
Message-ID: <20230424213011.528181-7-apinski@marvell.com> (raw)
In-Reply-To: <20230424213011.528181-1-apinski@marvell.com>
This factors out some of the code from the min/max detection
from match.pd into a function so it can be reused in other
places. This is mainly used to detect the conversions
of >= to > which causes the integer values to be changed by
one.
OK? Bootstrapped and tested on x86_64-linux-gnu.
gcc/ChangeLog:
* match.pd: Factor out the deciding the min/max from
the "(cond (cmp (convert1? x) c1) (convert2? x) c2)"
pattern to ...
* fold-const.cc (minmax_from_comparison): this new function.
* fold-const.h (minmax_from_comparison): New prototype.
---
gcc/fold-const.cc | 43 +++++++++++++++++++++++++++++++++++++++++++
gcc/fold-const.h | 3 +++
gcc/match.pd | 29 +----------------------------
3 files changed, 47 insertions(+), 28 deletions(-)
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 3b397ae2941..b8add24f874 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -150,6 +150,49 @@ static tree fold_convert_const (enum tree_code, tree, tree);
static tree fold_view_convert_expr (tree, tree);
static tree fold_negate_expr (location_t, tree);
+/* This is a helper function to detect min/max for some operands of COND_EXPR.
+ The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
+tree_code
+minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
+{
+ enum tree_code code = ERROR_MARK;
+
+ if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
+ return ERROR_MARK;
+
+ if (!operand_equal_p (exp0, exp2))
+ return ERROR_MARK;
+
+ if (TREE_CODE (exp1) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST
+ && wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
+ {
+ /* X <= Y - 1 equals to X < Y. */
+ if (cmp == LE_EXPR)
+ code = LT_EXPR;
+ /* X > Y - 1 equals to X >= Y. */
+ if (cmp == GT_EXPR)
+ code = GE_EXPR;
+ }
+ if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST
+ && wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
+ {
+ /* X < Y + 1 equals to X <= Y. */
+ if (cmp == LT_EXPR)
+ code = LE_EXPR;
+ /* X >= Y + 1 equals to X > Y. */
+ if (cmp == GE_EXPR)
+ code = GT_EXPR;
+ }
+ if (code != ERROR_MARK
+ || operand_equal_p (exp1, exp3))
+ {
+ if (cmp == LT_EXPR || cmp == LE_EXPR)
+ code = MIN_EXPR;
+ if (cmp == GT_EXPR || cmp == GE_EXPR)
+ code = MAX_EXPR;
+ }
+ return code;
+}
/* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
Otherwise, return LOC. */
diff --git a/gcc/fold-const.h b/gcc/fold-const.h
index 56ecaa87fc6..b828badc42f 100644
--- a/gcc/fold-const.h
+++ b/gcc/fold-const.h
@@ -246,6 +246,9 @@ extern tree fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE
#define fold_build_pointer_plus_hwi(p,o) \
fold_build_pointer_plus_hwi_loc (UNKNOWN_LOCATION, p, o)
+extern tree_code minmax_from_comparison (tree_code, tree, tree,
+ tree, tree);
+
/* In gimple-fold.cc. */
extern void clear_type_padding_in_mask (tree, unsigned char *);
extern bool clear_padding_type_may_have_padding_p (tree);
diff --git a/gcc/match.pd b/gcc/match.pd
index e89ba57e30b..6d3aaf45a93 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4677,34 +4677,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
|| TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
{
if (cmp != EQ_EXPR)
- {
- if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
- {
- /* X <= Y - 1 equals to X < Y. */
- if (cmp == LE_EXPR)
- code = LT_EXPR;
- /* X > Y - 1 equals to X >= Y. */
- if (cmp == GT_EXPR)
- code = GE_EXPR;
- }
- if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
- {
- /* X < Y + 1 equals to X <= Y. */
- if (cmp == LT_EXPR)
- code = LE_EXPR;
- /* X >= Y + 1 equals to X > Y. */
- if (cmp == GE_EXPR)
- code = GT_EXPR;
- }
- if (code != ERROR_MARK
- || wi::to_widest (@2) == wi::to_widest (@3))
- {
- if (cmp == LT_EXPR || cmp == LE_EXPR)
- code = MIN_EXPR;
- if (cmp == GT_EXPR || cmp == GE_EXPR)
- code = MAX_EXPR;
- }
- }
+ code = minmax_from_comparison (cmp, @1, @3, @1, @2);
/* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
else if (int_fits_type_p (@3, from_type))
code = EQ_EXPR;
--
2.39.1
next prev parent reply other threads:[~2023-04-24 21:30 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-24 21:30 [PATCH 0/7] Some more phiopt cleanups and double minmax to match Andrew Pinski
2023-04-24 21:30 ` [PATCH 1/7] PHIOPT: Split out store elimination from phiopt Andrew Pinski
2023-04-27 10:50 ` Richard Biener
2023-04-24 21:30 ` [PATCH 2/7] PHIOPT: Rename tree_ssa_phiopt_worker to pass_phiopt::execute Andrew Pinski
2023-04-27 10:58 ` Richard Biener
2023-04-24 21:30 ` [PATCH 3/7] PHIOPT: Move store_elim_worker into pass_cselim::execute Andrew Pinski
2023-04-27 10:50 ` Richard Biener
2023-04-24 21:30 ` [PATCH 4/7] MIN/MAX should be treated similar as comparisons for trapping Andrew Pinski
2023-04-27 10:49 ` Richard Biener
2023-04-24 21:30 ` [PATCH 5/7] PHIOPT: Allow MIN/MAX to have up to 2 MIN/MAX expressions for early phiopt Andrew Pinski
2023-04-27 10:51 ` Richard Biener
2023-04-24 21:30 ` Andrew Pinski [this message]
2023-04-25 10:45 ` [PATCH 6/7] MATCH: Factor out code that for min max detection with constants Mikael Morin
2023-04-24 21:30 ` [PATCH 7/7] MATCH: Add patterns from phiopt's minmax_replacement Andrew Pinski
2023-04-27 10:58 ` 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=20230424213011.528181-7-apinski@marvell.com \
--to=apinski@marvell.com \
--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).