public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: Richard Biener <rguenther@suse.de>, Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [PATCH] phiopt: Optimize (x <=> y) cmp z [PR94589]
Date: Fri, 14 May 2021 19:26:39 +0200	[thread overview]
Message-ID: <20210514172639.GW1179226@tucnak> (raw)
In-Reply-To: <4139aa5f-e3c3-c1ce-7fc3-a1e41a4d4c20@hippo.saclay.inria.fr>

On Thu, May 06, 2021 at 09:42:41PM +0200, Marc Glisse wrote:
> We can probably do it in 2 steps, first something like
> 
> (for cmp (eq ne)
>  (simplify
>   (cmp (bit_and:c @0 @1) @0)
>   (cmp (@0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); })))
> 
> to get rid of the double use, and then simplify X&C==0 to X<=~C if C is a
> mask 111...000 (I thought we already had a function to detect such masks, or
> the 000...111, but I can't find them anymore).
> 
> I agree that the comparison seems preferable, although if X is signed, the
> way GIMPLE represents types will add an inconvenient cast. And I think VRP
> already manages to use the bit test to derive a range.

I've tried the second step, but it unfortunately regresses
+FAIL: gcc.dg/ipa/propbits-2.c scan-tree-dump-not optimized "fail_test"
+FAIL: gcc.dg/tree-ssa/loop-42.c scan-tree-dump-not ivcanon "under assumptions "
so maybe it is better to keep these cases as the users wrote them.

So posting this patch just for archival purposes.

2021-05-13  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/94589
	* match.pd ((X & (-1 << N)) == 0 -> X <= (1 << N) - 1U): New
	simplification.

	* gcc.dg/tree-ssa/pr94589-2.c: New test.

--- gcc/match.pd.jj	2021-05-12 09:45:55.832976540 +0200
+++ gcc/match.pd	2021-05-13 19:51:17.458507854 +0200
@@ -4792,6 +4792,22 @@ (define_operator_list COND_TERNARY
   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
 
+#if GIMPLE
+/* (X & (-1 << N)) == 0 becomes X <= (1 << N) - 1.  */
+(for cmp (eq ne)
+     ncmp (le gt)
+ (simplify
+  (cmp:c (bit_and:cs @0 INTEGER_CST@1) integer_zerop)
+  (with { tree utype = NULL_TREE;
+	  int tz = wi::ctz (wi::to_wide (@1));
+	  int prec = TYPE_PRECISION (TREE_TYPE (@1));
+	  if (tz && wi::eq_p (wi::shifted_mask (tz, prec - tz, false, prec),
+			      wi::to_wide (@1)))
+	    utype = unsigned_type_for (TREE_TYPE (@0)); }
+   (if (utype)
+    (ncmp (convert:utype @0) (convert:utype (bit_not @1)))))))
+#endif
+
 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
--- gcc/testsuite/gcc.dg/tree-ssa/pr94589-2.c.jj	2021-05-13 19:55:21.201854003 +0200
+++ gcc/testsuite/gcc.dg/tree-ssa/pr94589-2.c	2021-05-13 19:54:59.804086977 +0200
@@ -0,0 +1,21 @@
+/* PR tree-optimization/94589 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int x)
+{
+  return (x & (-1 << 7)) == 0;
+/* { dg-final { scan-tree-dump " = \\\(unsigned int\\\) x_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " & -128" "optimized" } } */
+/* { dg-final { scan-tree-dump " <= 127" "optimized" } } */
+}
+
+int
+bar (int y)
+{
+  return (y & (-1 << 12)) != 0;
+/* { dg-final { scan-tree-dump " = \\\(unsigned int\\\) y_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " & -4096" "optimized" } } */
+/* { dg-final { scan-tree-dump " > 4095" "optimized" } } */
+}


	Jakub


  parent reply	other threads:[~2021-05-14 17:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04  7:44 Jakub Jelinek
2021-05-04  9:42 ` Jonathan Wakely
2021-05-04  9:56   ` Jakub Jelinek
2021-05-05 11:39 ` Richard Biener
2021-05-05 13:18   ` [PATCH] phiopt, v2: " Jakub Jelinek
2021-05-05 14:17     ` Richard Biener
2021-05-05 11:45 ` [PATCH] phiopt: " Marc Glisse
2021-05-05 16:52   ` Jakub Jelinek
2021-05-06 10:22     ` Jakub Jelinek
2021-05-06 19:42       ` Marc Glisse
2021-05-11  7:34         ` [PATCH] match.pd: Optimize (x & y) == x into (x & ~y) == 0 [PR94589] Jakub Jelinek
2021-05-11  8:11           ` Marc Glisse
2021-05-11  8:20           ` Richard Biener
2021-05-14 17:26         ` Jakub Jelinek [this message]
2021-05-15 10:09           ` [PATCH] phiopt: Optimize (x <=> y) cmp z [PR94589] Marc Glisse
2021-05-05 15:45 ` Martin Sebor

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=20210514172639.GW1179226@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=rguenther@suse.de \
    /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).