public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eugene Rozenfeld <Eugene.Rozenfeld@microsoft.com>
To: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: [PATCH] [tree-optimization] Optimize max/min pattern with comparison
Date: Wed, 25 Nov 2020 22:04:50 +0000	[thread overview]
Message-ID: <BYAPR21MB1351C1B1A5B690E3D16A279791FA1@BYAPR21MB1351.namprd21.prod.outlook.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 495 bytes --]

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

[-- Attachment #2: 0001-Optimize-max-pattern-with-comparison.patch --]
[-- Type: application/octet-stream, Size: 1272 bytes --]

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); } ))
+
 /* Undo fancy way of writing max/min or other ?: expressions,
    like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
    People normally use ?: and that is what we actually try to optimize.  */
-- 
2.17.1


             reply	other threads:[~2020-11-25 22:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-25 22:04 Eugene Rozenfeld [this message]
2020-11-30 17:51 ` Jeff Law
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=BYAPR21MB1351C1B1A5B690E3D16A279791FA1@BYAPR21MB1351.namprd21.prod.outlook.com \
    --to=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).