From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id D5D003858D37 for ; Fri, 28 Apr 2023 10:36:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5D003858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-4f00d41df22so9131160e87.1 for ; Fri, 28 Apr 2023 03:36:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682678165; x=1685270165; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=iuEJyl372TJzYM6SiGn1PKX7oaG2038EatYTkKcec2Q=; b=deErionlhUg5rgKiXMeboShUwlu3ZDwl8OLhwpI0AMJeZpE2Kn32/7bQdNBzt0BlKG ++TVkLVYAZakiQiJHdz/uoc8l90BZUD82Kzu133ILCeaUaPAnaqS4xV7yiIhAB6P3CI+ Ijm3CWczAhi8BSKXz0sJsf8uC+yeHaq+nwxQpPRaX6BjmaalAp6gmhsxW7lo70xF9Wk+ EuTijSz7u7Q2CgOg9hsaU3RC7QUgKFHlIBQsVMT17daMg8ALn6Ty4Ka614sx6v+NkLtI XVbbgZfhCYD1vmgHDtlurrMdPCqT9hUeUx9J8+C7C14hYJkxBMN7cCloCek+3fBnNFhG F0fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682678165; x=1685270165; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iuEJyl372TJzYM6SiGn1PKX7oaG2038EatYTkKcec2Q=; b=EQ3uxlyRmbGR9a5WMwKYPs+fSCeDUMB0H9N65wio7yFkQ5r/vnLFewxBp7jZsdb/Ku aLf5hBkxEZdH0VzR3+DejMcMMbB3F2OGQOnaaV7f2kbtB3GURfuvaam0ZWa9H/u4VklJ ojZoZcaHLy9TmDB0+lJoTIyuRiqRSfV7IqcvipdCvx+tXg7V+NJY8p88xQxbdoL3u4Nw dI4iEDTtW4UN1G2bQ09/aU0u0c8SrjzwutipQlfyEEmSdcXdhClw/uiF0j7AS5/f7QS8 /pUAsRLFyNDlEkNQvcfV8W6wxo23nvVmLllyGdicyCxTvi+7+sCIbKJOJY52EqajRPhg 3Wvg== X-Gm-Message-State: AC+VfDzpf9CY/B1Mol00w6GPdrcM1UMY/CuwKHSJsQcywWo5+/Df6XPv 9EkhNZI7QUmriyPaP6eWKatUmIJs5o6ajNAsMww= X-Google-Smtp-Source: ACHHUZ59ftq3zMiknMeRtmpxrAWs0CdkO9XwpRQ+GjZHQU27UFp21eOP/8XTvlmpaRNBWk5ylqL+dL0s/vK6CD2HKz8= X-Received: by 2002:a05:651c:1243:b0:2a8:a631:5ec4 with SMTP id h3-20020a05651c124300b002a8a6315ec4mr2642565ljh.14.1682678164772; Fri, 28 Apr 2023 03:36:04 -0700 (PDT) MIME-Version: 1.0 References: <20230428032506.655667-1-apinski@marvell.com> In-Reply-To: <20230428032506.655667-1-apinski@marvell.com> From: Richard Biener Date: Fri, 28 Apr 2023 12:34:32 +0200 Message-ID: Subject: Re: [PATCHv2] MATCH: Factor out code that for min max detection with constants To: Andrew Pinski Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Apr 28, 2023 at 5:25=E2=80=AFAM Andrew Pinski via Gcc-patches wrote: > > 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 >=3D 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. OK. > 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 | 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 =3D 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) =3D=3D INTEGER_CST && TREE_CODE (exp1) =3D=3D INT= EGER_CST) > + { > + if (wi::to_widest (exp1) =3D=3D (wi::to_widest (exp3) - 1)) > + { > + /* X <=3D Y - 1 equals to X < Y. */ > + if (cmp =3D=3D LE_EXPR) > + code =3D LT_EXPR; > + /* X > Y - 1 equals to X >=3D Y. */ > + if (cmp =3D=3D GT_EXPR) > + code =3D GE_EXPR; > + } > + if (wi::to_widest (exp1) =3D=3D (wi::to_widest (exp3) + 1)) > + { > + /* X < Y + 1 equals to X <=3D Y. */ > + if (cmp =3D=3D LT_EXPR) > + code =3D LE_EXPR; > + /* X >=3D Y + 1 equals to X > Y. */ > + if (cmp =3D=3D GE_EXPR) > + code =3D GT_EXPR; > + } > + } > + if (code !=3D ERROR_MARK > + || operand_equal_p (exp1, exp3)) > + { > + if (cmp =3D=3D LT_EXPR || cmp =3D=3D LE_EXPR) > + code =3D MIN_EXPR; > + if (cmp =3D=3D GT_EXPR || cmp =3D=3D GE_EXPR) > + code =3D 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) =3D=3D TYPE_SIGN (from_type))= ))) > { > if (cmp !=3D EQ_EXPR) > - { > - if (wi::to_widest (@3) =3D=3D (wi::to_widest (@2) - 1)) > - { > - /* X <=3D Y - 1 equals to X < Y. */ > - if (cmp =3D=3D LE_EXPR) > - code =3D LT_EXPR; > - /* X > Y - 1 equals to X >=3D Y. */ > - if (cmp =3D=3D GT_EXPR) > - code =3D GE_EXPR; > - } > - if (wi::to_widest (@3) =3D=3D (wi::to_widest (@2) + 1)) > - { > - /* X < Y + 1 equals to X <=3D Y. */ > - if (cmp =3D=3D LT_EXPR) > - code =3D LE_EXPR; > - /* X >=3D Y + 1 equals to X > Y. */ > - if (cmp =3D=3D GE_EXPR) > - code =3D GT_EXPR; > - } > - if (code !=3D ERROR_MARK > - || wi::to_widest (@2) =3D=3D wi::to_widest (@3)) > - { > - if (cmp =3D=3D LT_EXPR || cmp =3D=3D LE_EXPR) > - code =3D MIN_EXPR; > - if (cmp =3D=3D GT_EXPR || cmp =3D=3D GE_EXPR) > - code =3D MAX_EXPR; > - } > - } > + code =3D minmax_from_comparison (cmp, @1, @3, @1, @2); > /* Can do A =3D=3D C1 ? A : C2 -> A =3D=3D C1 ? C1 : C2? */ > else if (int_fits_type_p (@3, from_type)) > code =3D EQ_EXPR; > -- > 2.31.1 >