From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1314) id 757573858D37; Fri, 28 Apr 2023 14:27:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 757573858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682692050; bh=Slcv0klQNiaafq2d3HbCBitOPsmCE/XVIjUQoL8iqno=; h=From:To:Subject:Date:From; b=kVeb7/4NfQqTlA0fTXCM4rh1WY09OGDSmj/jAUWFnuG+kSjzLKNd2CQQ3+z+IrMa8 mENOdcBoDhRq2CJmMQ54zLWKwjO2yyF6j6+t1uPco2zgewv2++qhMb2zQt2B/13Z2/ TEpym4dAvIRhB5MOQBz3dccma3O97/x/sFAJGqNA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Andrew Pinski To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-336] MATCH: Factor out code that for min max detection with constants X-Act-Checkin: gcc X-Git-Author: Andrew Pinski X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 650c36ec461a722d9c65e82512b4c3aeec2ffee1 X-Git-Newrev: b9b30dbaabc8076d9265cf685f45d3b0072a09df Message-Id: <20230428142730.757573858D37@sourceware.org> Date: Fri, 28 Apr 2023 14:27:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b9b30dbaabc8076d9265cf685f45d3b0072a09df commit r14-336-gb9b30dbaabc8076d9265cf685f45d3b0072a09df Author: Andrew Pinski Date: Sun Apr 2 17:42:58 2023 +0000 MATCH: Factor out code that for min max detection with constants 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. Changes since v1: * factor out the checks for INTEGER_CSTs so it is more obvious. 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. Diff: --- gcc/fold-const.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ gcc/fold-const.h | 3 +++ gcc/match.pd | 29 +---------------------------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 3b397ae2941..7d2352dbcdd 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -150,6 +150,50 @@ 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 (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST) + { + if (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 (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 c4320781f5b..fad337a9697 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;