From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id AF9AF3858C5E for ; Tue, 30 May 2023 09:34:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AF9AF3858C5E 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-x12a.google.com with SMTP id 2adb3069b0e04-4f4db9987f8so5678724e87.1 for ; Tue, 30 May 2023 02:34:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685439288; x=1688031288; 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=kKmZxTW42tWWRgHv+msB2oCyDpamOQz1pCSF9f2r8HE=; b=bEZJ5QU5OxcxkDKmgJsPhZPwRoJ7Y7BH4/9h1jg5wuMhkRAHlqYPP2nEBb3uxXkE8i FlkFb5OAU+eVdXKjLnGPTwcY6pL28Rtp/6EgAKH6749glvhd/tsN9ISCZ2YnVjXPnoDr U3n99wRmIRWds+OLiDzL2rZRwftO87KXNuDEXQiwUESWWhq1gGSQZq2MQGTAdAZFCDs3 z5DEhib15MF7nE9c1l2N9RZo2XXEM0mph8tYFw0aqpEy89zf4sNwqetOTl6uFHRDH0K2 gNcVoBqrWTa0k5gn++D+HDFOwYI5db6fZURlFZORMkSaknX0HPG+wegYJEJwEE75+wjI mjGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685439288; x=1688031288; 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=kKmZxTW42tWWRgHv+msB2oCyDpamOQz1pCSF9f2r8HE=; b=DRODmUKJUPqZn2g0OEuH2GsYz3YOgeBchbbFL6JyaCQNl1C3FFOH/vXHMkqLgl89+a V2I1hdB/KiKZaBE4UOjy7J/FmwsksurlxJBCAP3Cg4cdfu0jBfk1bjgKir515MBPnZcP 6O2+5eavaCs8FtvZJDGNUCfM3pBDL+Kgu88m+OBtPUlu6m3+77WXD3alFntkretSt992 YYx34ES6xvoaKqyml1GqRgU5D469R7Av8pkf/C0za5Uf/blOjgI+b+ihjx20M8E0Jbx0 oeDLWkLb1XgM2aB/8w+nHZ+DPOjr2PiySp8Rmx6xjO9O/0TS5otJfh/XysG3GQNvrVeh keGg== X-Gm-Message-State: AC+VfDyZgQxV+BymBBqIZe8HygIcoDTGL8wXwRyTbjEk83jmlY6HKBQF TxVmD7ScK7nnKZ4bP2eG6AtJWwGUWu5iKtlIHBqf2UEL X-Google-Smtp-Source: ACHHUZ69CuEnEyUs7lPnSNY5BVHT0kqrEn9xLqp2BqsPYXdL1AHzG9M658nD2/fj8P74QG1u7QAtctkCeydN1Lh9adg= X-Received: by 2002:a2e:88c5:0:b0:2a9:ec7e:8f58 with SMTP id a5-20020a2e88c5000000b002a9ec7e8f58mr581497ljk.7.1685439288010; Tue, 30 May 2023 02:34:48 -0700 (PDT) MIME-Version: 1.0 References: <20230508052625.1185434-1-apinski@marvell.com> In-Reply-To: <20230508052625.1185434-1-apinski@marvell.com> From: Richard Biener Date: Tue, 30 May 2023 11:32:35 +0200 Message-ID: Subject: Re: [PATCH] Add a != MIN/MAX_VALUE_CST ? CST-+1 : a to minmax_from_comparison 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.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,PLING_QUERY,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 Mon, May 8, 2023 at 7:27=E2=80=AFAM Andrew Pinski via Gcc-patches wrote: > > This patch adds the support for match that was implemented for PR 87913 i= n phiopt. > It implements it by adding support to minmax_from_comparison for the chec= k. > It uses the range information if available which allows to produce MIN/MA= X expression > when comparing against the lower/upper bound of the range instead of lowe= r/upper > of the type. > > minmax-20.c is the new testcase which tests the ranges part. > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. OK. > gcc/ChangeLog: > > * fold-const.cc (minmax_from_comparison): Add support for NE_EXPR= . > * match.pd ((cond (cmp (convert1? x) c1) (convert2? x) c2) patter= n): > Add ne as a possible cmp. > ((a CMP b) ? minmax : minmax pattern): Likewise. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/minmax-20.c: New test. > --- > gcc/fold-const.cc | 26 +++++++++++++++++++++++ > gcc/match.pd | 4 ++-- > gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c | 12 +++++++++++ > 3 files changed, 40 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c > > diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc > index db54bfc5662..d90671b9975 100644 > --- a/gcc/fold-const.cc > +++ b/gcc/fold-const.cc > @@ -173,6 +173,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tr= ee exp1, tree exp2, tree exp > /* X > Y - 1 equals to X >=3D Y. */ > if (cmp =3D=3D GT_EXPR) > code =3D GE_EXPR; > + /* a !=3D MIN_RANGE ? a : MIN_RANGE+1 -> MAX_EXPR+1, a> */ > + if (cmp =3D=3D NE_EXPR && TREE_CODE (exp0) =3D=3D SSA_NAME) > + { > + value_range r; > + get_range_query (cfun)->range_of_expr (r, exp0); > + if (r.undefined_p ()) > + r.set_varying (TREE_TYPE (exp0)); > + > + widest_int min =3D widest_int::from (r.lower_bound (), > + TYPE_SIGN (TREE_TYPE (ex= p0))); > + if (min =3D=3D wi::to_widest (exp1)) > + code =3D MAX_EXPR; > + } > } > if (wi::to_widest (exp1) =3D=3D (wi::to_widest (exp3) + 1)) > { > @@ -182,6 +195,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tr= ee exp1, tree exp2, tree exp > /* X >=3D Y + 1 equals to X > Y. */ > if (cmp =3D=3D GE_EXPR) > code =3D GT_EXPR; > + /* a !=3D MAX_RANGE ? a : MAX_RANGE-1 -> MIN_EXPR-1, a> */ > + if (cmp =3D=3D NE_EXPR && TREE_CODE (exp0) =3D=3D SSA_NAME) > + { > + value_range r; > + get_range_query (cfun)->range_of_expr (r, exp0); > + if (r.undefined_p ()) > + r.set_varying (TREE_TYPE (exp0)); > + > + widest_int max =3D widest_int::from (r.upper_bound (), > + TYPE_SIGN (TREE_TYPE (ex= p0))); > + if (max =3D=3D wi::to_widest (exp1)) > + code =3D MIN_EXPR; > + } > } > } > if (code !=3D ERROR_MARK > diff --git a/gcc/match.pd b/gcc/match.pd > index a55ede838cd..95f7e9a6abc 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -4751,7 +4751,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > Case 2) > (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c= 2). */ > -(for cmp (lt le gt ge eq) > +(for cmp (lt le gt ge eq ne) > (simplify > (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) > (with > @@ -4942,7 +4942,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* Optimize (a CMP b) ? minmax : minmax > to minmax, c> */ > (for minmax (min max) > - (for cmp (lt le gt ge) > + (for cmp (lt le gt ge ne) > (simplify > (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4)) > (with > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c b/gcc/testsuite/gc= c.dg/tree-ssa/minmax-20.c > new file mode 100644 > index 00000000000..481c375f5f9 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-phiopt2" } */ > + > +int f(int num) > +{ > + if (num < 3) __builtin_unreachable(); > + return num !=3D 3 ? num : 4; > +} > + > +/* In phiopt2 with the range information, this should be turned into > + a MAX_EXPR. */ > +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt2" } } */ > -- > 2.31.1 >