public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Eugene Rozenfeld <Eugene.Rozenfeld@microsoft.com>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] [tree-optimization] Optimize max/min pattern with comparison
Date: Mon, 30 Nov 2020 10:51:14 -0700	[thread overview]
Message-ID: <d8793789-32d6-029c-79a3-f91014806593@redhat.com> (raw)
In-Reply-To: <BYAPR21MB1351C1B1A5B690E3D16A279791FA1@BYAPR21MB1351.namprd21.prod.outlook.com>



On 11/25/20 3:04 PM, Eugene Rozenfeld via Gcc-patches wrote:
> Make the following simplifications:
>     X <= MAX(X, Y) -> true
>     X > MAX(X, Y) -> false
>     X >= MIN(X, Y) -> true
>     X < MIN(X, Y) -> false
>     
> This fixes PR96708.
>     
> Tested on x86_64-pc-linux-gnu.
>
> bool f(int a, int b)
> {
>     int tmp = (a < b) ? b : a;
>     return tmp >= a;
> }
>
> Code without the patch:
>
> vmovd  xmm0,edi
> vmovd  xmm1,esi
> vpmaxsd xmm0,xmm0,xmm1
> vmovd  eax,xmm0
> cmp    eax,edi
> setge  al
> ret    
>
> Code with the patch:
>
> mov    eax,0x1
> ret
>
> Eugene
>
> 0001-Optimize-max-pattern-with-comparison.patch
>
> From f6391c197b670b516238ac7707512c1358336520 Mon Sep 17 00:00:00 2001
> From: Eugene Rozenfeld <erozen@microsoft.com>
> Date: Sat, 21 Nov 2020 01:08:50 -0800
> Subject: [PATCH] Optimize max pattern with comparison
>
> Make the following simplifications:
> X <= MAX(X, Y) -> true
> X > MAX(X, Y) -> false
> X >= MIN(X, Y) -> true
> X < MIN(X, Y) -> false
>
> This fixes PR96708.
>
> gcc/
> * match.pd : New patterns.
> ---
>  gcc/match.pd | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index cbb4bf0b32d..75237741946 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2851,6 +2851,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>    (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
>    (comb (cmp @0 @2) (cmp @1 @2))))
>  
> +/* X <= MAX(X, Y) -> true
> +   X > MAX(X, Y) -> false 
> +   X >= MIN(X, Y) -> true
> +   X < MIN(X, Y) -> false */
> +(for minmax (min     min     max     max     )
> +     cmp    (ge      lt      le      gt      )
> + (simplify
> +  (cmp @0 (minmax:c @0 @1))
> +  { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
Don't you need to look at the opcode (MIN vs MAX) vs CMP to know the
result?  I'd really like to see some tests for the testsuite.    In
particular I'd like to see positive tests where we should apply the
optimization and negative tests when we should not apply the optimization.

I also wonder if there's value in handling this in Ranger and/or DOM. 
Though I'd probably wait to see if fixing in match.pd is sufficient to
cover the cases I'm thinking of in Ranger & DOM.

Jeff



  reply	other threads:[~2020-11-30 17:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-25 22:04 Eugene Rozenfeld
2020-11-30 17:51 ` Jeff Law [this message]
2020-12-01  2:00 Eugene Rozenfeld
2020-12-01  6:30 ` Jeff Law
2020-12-01 23:29 ` Jeff Law
2020-12-06  9:29 ` Marc Glisse

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=d8793789-32d6-029c-79a3-f91014806593@redhat.com \
    --to=law@redhat.com \
    --cc=Eugene.Rozenfeld@microsoft.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).