From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by sourceware.org (Postfix) with ESMTPS id D02B63858C00 for ; Tue, 30 May 2023 09:33:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D02B63858C00 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-x136.google.com with SMTP id 2adb3069b0e04-4effb818c37so4540306e87.3 for ; Tue, 30 May 2023 02:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685439217; x=1688031217; 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=pRgcmDbSwpg/DKK9PMi+L+uAz/99fhp2GcJTdwRiI0c=; b=qbrAK26OUaLVL4Wg5E6wQaMqnLKOBOHfTtxbse9calWTFFnSb+DUz++o+YygMsFxYs zp9A77WlGlPFTZViMrFH9n2d6VMbOASWj6WxPtbdeTngsn3MY0CDhMF+cYuodSyjRoMk bDtO4rNkbqbovy09fEJ6TuZjDV9XBkehiTmMZxhLgnD6pWprIUx3kFlEc0o2l2lVdobO yAWsoRwKw+Wsy/HSEvWAnJ5xtu4hmG0FkdGXI9RTNVQguz5TZ/ioHWCDT0HFcvFOhIXx 6Oa9vm6dwXImi5brbNc7J3MRd/YguL+ZYXNNQdGUVSFs2qIJFYm3S/Mj2B4ETo4NpPd6 dp/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685439217; x=1688031217; 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=pRgcmDbSwpg/DKK9PMi+L+uAz/99fhp2GcJTdwRiI0c=; b=aWn+lFvlxrnmQh/hMe+vbUhOUcFJEUuB8fdOZO8hfS6DhxKdRafpdHavz7oirmosGa r9N2QBHCE/mfwgFhTyj7F31D/G1U3NsuTCcj0DmHIe54izZ6+C5ORLtpSdkauApxWhdH 5AefgihdsN6EdxdTvfWa71gtljMgzcP1MN9ULwt+SmZ8k+dDx/zoEt6OCTimrnKd6OW0 3T0fK3O9cmG5uPzp/rtBQ6+F+hHaYNEssh5UdZ1EaCBdauPJTXMvzOwYScYdQCdk3mnI cYZfKcerjDfYGNEzHTkkyqtIl/YGJ0gusPPCzSVZLiX+6H0TO0xsos8ePid7mBjdPWjq 8VnA== X-Gm-Message-State: AC+VfDy5uNxAW+EgVx4G2F5MNYueRp9NmFdPWZtxohTfGoIAYoJbXz7A 19n5esuFG9DP3eDpa1OU/kjHEwlgBDAxn673tfd+Rvm6 X-Google-Smtp-Source: ACHHUZ71uNJ2zfnpHpWyjArJ01eurWRx3FbCLix8gnsd03Vv085mEtdtNihS1vpRB6MlQT9DRVJTXpzK/1EgqkHv1pk= X-Received: by 2002:a2e:99cb:0:b0:2af:e006:b83 with SMTP id l11-20020a2e99cb000000b002afe0060b83mr489179ljj.18.1685439216868; Tue, 30 May 2023 02:33:36 -0700 (PDT) MIME-Version: 1.0 References: <20230507221959.1166993-1-apinski@marvell.com> In-Reply-To: <20230507221959.1166993-1-apinski@marvell.com> From: Richard Biener Date: Tue, 30 May 2023 11:31:24 +0200 Message-ID: Subject: Re: [PATCH] MATCH: Move `a <= CST1 ? MAX : a` optimization to match 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 Mon, May 8, 2023 at 12:21=E2=80=AFAM Andrew Pinski via Gcc-patches wrote: > > This moves the `a <=3D CST1 ? MAX : a` optimization > from phiopt to match. It just adds a new pattern to match.pd. > > There is one more change needed before being able to remove > minmax_replacement from phiopt. > > A few notes on the testsuite changes: > * phi-opt-5.c is now able to optimize at phiopt1 so remove > the xfail. > * pr66726-4.c can be optimized during fold before phiopt1 > so need to change the scanning. > * pr66726-5.c needs two phiopt passes currently to optimize > to the right thing, it needed 2 phiopt passes before, the cast > from int to unsigned char is the reason. > * pr66726-6.c is what the original pr66726-4.c was testing > before the fold was able to optimize it. > > OK? Bootstrapped and tested on x86_64-linux-gnu. OK. > gcc/ChangeLog: > > * match.pd (`(a CMP CST1) ? max : a`): New > pattern. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/phi-opt-5.c: Remove last xfail. > * gcc.dg/tree-ssa/pr66726-4.c: Change how scanning > works. > * gcc.dg/tree-ssa/pr66726-5.c: New test. > * gcc.dg/tree-ssa/pr66726-6.c: New test. > --- > gcc/match.pd | 18 +++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c | 2 +- > gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c | 5 +++- > gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c | 28 +++++++++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c | 17 ++++++++++++++ > 5 files changed, 68 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index ceae1c34abc..a55ede838cd 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -4954,6 +4954,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (code =3D=3D MAX_EXPR) > (minmax (max @1 @2) @4))))))) > > +/* Optimize (a CMP CST1) ? max : a */ > +(for cmp (gt ge lt le) > + minmax (min min max max) > + (simplify > + (cond (cmp @0 @1) (minmax:c@2 @0 @3) @4) > + (with > + { > + tree_code code =3D minmax_from_comparison (cmp, @0, @1, @0, @4); > + } > + (if ((cmp =3D=3D LT_EXPR || cmp =3D=3D LE_EXPR) > + && code =3D=3D MIN_EXPR > + && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @= 3, @1))) > + (min @2 @4) > + (if ((cmp =3D=3D GT_EXPR || cmp =3D=3D GE_EXPR) > + && code =3D=3D MAX_EXPR > + && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, = @3, @1))) > + (max @2 @4)))))) > + > /* X !=3D C1 ? -X : C2 simplifies to -X when -C1 =3D=3D C2. */ > (simplify > (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2) > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c b/gcc/testsuite/gc= c.dg/tree-ssa/phi-opt-5.c > index 5f78a1ba6dc..e78d9d8b83d 100644 > --- a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-5.c > @@ -39,7 +39,7 @@ float repl2 (float vary) > > /* phiopt1 confused by predictors. */ > /* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt1" } } */ > -/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" { xfail= *-*-* } } } */ > +/* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt1" } } */ > /* { dg-final { scan-tree-dump "vary.*MAX_EXPR.*0\\.0" "phiopt2"} } */ > /* { dg-final { scan-tree-dump "vary.*MIN_EXPR.*1\\.0" "phiopt2"} } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c b/gcc/testsuite/gc= c.dg/tree-ssa/pr66726-4.c > index 4e43522f3a3..930ad5fb79f 100644 > --- a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-4.c > @@ -9,4 +9,7 @@ foo (unsigned char *p, int i) > *p =3D SAT (i); > } > > -/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to= straightline code" 1 "phiopt1" } } */ > +/* fold could optimize SAT before phiopt1 so only match on the > + MIN/MAX here. */ > +/* { dg-final { scan-tree-dump-times "=3D MIN_EXPR" 1 "phiopt1" } } */ > +/* { dg-final { scan-tree-dump-times "=3D MAX_EXPR" 1 "phiopt1" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c b/gcc/testsuite/gc= c.dg/tree-ssa/pr66726-5.c > new file mode 100644 > index 00000000000..4b5066cdb6b > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-5.c > @@ -0,0 +1,28 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-phiopt1-details -fdump-tree-phiopt2-det= ails -fdump-tree-optimized" } */ > + > +#define SAT(x) (x < 0 ? 0 : (x > 255 ? 255 : x)) > + > +unsigned char > +foo (unsigned char *p, int i) > +{ > + if (i < 0) > + return 0; > + { > + int t; > + if (i > 255) > + t =3D 255; > + else > + t =3D i; > + return t; > + } > +} > + > +/* Because of the way PHIOPT works, it only does the merging of BBs afte= r it is done so we get the case were we can't > + optimize the above until phiopt2 right now. */ > +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to= straightline code" 2 "phiopt1" { xfail *-*-* } } } */ > +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to= straightline code" 0 "phiopt2" { xfail *-*-* } } } */ > +/* { dg-final { scan-tree-dump-times "=3D MIN_EXPR" 1 "phiopt1" } } */ > +/* { dg-final { scan-tree-dump-times "=3D MAX_EXPR" 1 "phiopt1" { xfail = *-*-* } } } */ > +/* { dg-final { scan-tree-dump-times "=3D MIN_EXPR" 1 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times "=3D MAX_EXPR" 1 "optimized" } } *= / > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c b/gcc/testsuite/gc= c.dg/tree-ssa/pr66726-6.c > new file mode 100644 > index 00000000000..5c6b4992608 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr66726-6.c > @@ -0,0 +1,17 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-phiopt1-details" } */ > + > + > +unsigned char > +foo1 (unsigned char *p, int i) > +{ > + if (i < 0) > + return 0; > + { > + int t =3D i > 255 ? 255 : i; > + return t; > + } > +} > +/* testing to see if moving the cast out of the conditional. */ > + > +/* { dg-final { scan-tree-dump-times "COND_EXPR .*and PHI .*converted to= straightline code" 1 "phiopt1" } } */ > -- > 2.31.1 >