public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <richard.guenther@gmail.com>,
	Drew Ross <drewross7@gmail.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] match.pd, v2: Implement missed optimization ((x ^ y) & z) | x -> (z & y) | x [PR109938]
Date: Thu, 10 Aug 2023 17:43:02 +0200	[thread overview]
Message-ID: <ZNUFhqrSn3Gcq09w@tucnak> (raw)
In-Reply-To: <ZNS7yHDo6fc85LO0@tucnak>

Hi!

On Thu, Aug 10, 2023 at 12:28:24PM +0200, Jakub Jelinek via Gcc-patches wrote:
> On Tue, Aug 08, 2023 at 03:18:51PM +0200, Richard Biener via Gcc-patches wrote:
> > On Fri, Aug 4, 2023 at 11:49 PM Drew Ross via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > >
> > > Adds a simplification for ((x ^ y) & z) | x to be folded into
> > > (z & y) | x. Merges this simplification with ((x | y) & z) | x -> (z & y) | x
> > > to prevent duplicate pattern. Tested successfully on x86_64 and x86 targets.
> > 
> > OK.
> 
> Shouldn't
>   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
> be changed to
>   (bit_ior:c (nop_convert1?:s
> 	       (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
> rather than
>   (bit_ior:c (nop_convert1? (bit_and:c (nop_convert2? (op:c @0 @1)) @2)) @3)
> in the patch?
> I mean the :s modifiers were there for a reason, if some of the
> intermediates aren't a single use, then the simplification doesn't simplify
> anything and can even make things larger.

Here it is in patch form.  Bootstrapped/regtested on x86_64-linux and
i686-linux, ok for trunk?

2023-08-10  Drew Ross  <drross@redhat.com>
	    Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/109938
        * match.pd (((x ^ y) & z) | x -> (z & y) | x): New simplification.

	* gcc.c-torture/execute/pr109938.c: New test.
	* gcc.dg/tree-ssa/pr109938.c: New test.

--- gcc/match.pd.jj	2023-08-10 09:26:19.390805079 +0200
+++ gcc/match.pd	2023-08-10 13:33:17.959654775 +0200
@@ -1972,10 +1972,14 @@ (define_operator_list SYNC_FETCH_AND_AND
   (if (bitwise_inverted_equal_p (@0, @2))
    (bitop @0 @1))))
 
-/* ((x | y) & z) | x -> (z & y) | x */
-(simplify
-  (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
-  (bit_ior (bit_and @2 @1) @0))
+/* ((x | y) & z) | x -> (z & y) | x
+   ((x ^ y) & z) | x -> (z & y) | x  */
+(for op (bit_ior bit_xor)
+ (simplify
+  (bit_ior:c (nop_convert1?:s
+	       (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
+  (if (bitwise_equal_p (@0, @3))
+   (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
 
 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
 (simplify
--- gcc/testsuite/gcc.dg/tree-ssa/pr109938.c.jj	2023-08-10 13:22:19.513095403 +0200
+++ gcc/testsuite/gcc.dg/tree-ssa/pr109938.c	2023-08-10 13:35:24.428841774 +0200
@@ -0,0 +1,125 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-dse2 -Wno-psabi" } */
+
+typedef int v4si __attribute__((vector_size(4 * sizeof(int))));
+
+/* Generic */
+__attribute__((noipa)) int
+t1 (int a, int b, int c)
+{
+  return ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) unsigned int
+t2 (int a, unsigned int b, int c)
+{
+  return ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) unsigned long
+t3 (unsigned long a, long b, unsigned long c)
+{
+  return ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) unsigned short
+t4 (short a, unsigned short b, unsigned short c)
+{
+  return (unsigned short) ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) unsigned char
+t5 (unsigned char a, signed char b, signed char c)
+{
+  return ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) long long
+t6 (long long a, long long b, long long c)
+{
+  return ((a ^ c) & (unsigned long long) b) | a;
+}
+
+/* Gimple */
+__attribute__((noipa)) int
+t7 (int a, int b, int c)
+{
+  int t1 = a ^ c;
+  int t2 = t1 & b;
+  int t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) int
+t8 (int a, unsigned int b, unsigned int c)
+{
+  unsigned int t1 = a ^ c;
+  int t2 = t1 & b;
+  int t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) unsigned int
+t9 (unsigned int a, unsigned int b, int c)
+{
+  unsigned int t1 = a ^ c;
+  unsigned int t2 = t1 & b;
+  unsigned int t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) unsigned long
+t10 (unsigned long a, long b, unsigned long c)
+{
+  unsigned long t1 = a ^ c;
+  unsigned long t2 = t1 & b;
+  unsigned long t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) unsigned short
+t11 (short a, unsigned short b, short c)
+{
+  short t1 = a ^ c;
+  unsigned short t2 = t1 & b;
+  unsigned short t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) unsigned char
+t12 (signed char a, unsigned char b, signed char c)
+{
+  unsigned char t1 = a ^ c;
+  unsigned char t2 = t1 & b;
+  unsigned char t3 = t2 | a;
+  return t3;
+}
+
+__attribute__((noipa)) unsigned long long
+t13 (unsigned long long a, long long b, unsigned long long c)
+{
+  long long t1 = a ^ c;
+  long long t2 = t1 & b;
+  unsigned long long t3 = t2 | a;
+  return t3;
+}
+
+/* Vectors */
+__attribute__((noipa)) v4si
+t14 (v4si a, v4si b, v4si c)
+{
+  return ((a ^ c) & b) | a;
+}
+
+__attribute__((noipa)) v4si
+t15 (v4si a, v4si b, v4si c)
+{
+  v4si t1 = a ^ c;
+  v4si t2 = t1 & b;
+  v4si t3 = t2 | a;
+  return t3;
+}
+
+/* { dg-final { scan-tree-dump-not " \\\^ " "dse2" } } */
+/* { dg-final { scan-tree-dump-times " \\\| " 15 "dse2" } } */
+/* { dg-final { scan-tree-dump-times " & " 15 "dse2" } } */
--- gcc/testsuite/gcc.c-torture/execute/pr109938.c.jj	2023-08-10 13:22:19.513095403 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr109938.c	2023-08-10 13:22:19.513095403 +0200
@@ -0,0 +1,33 @@
+/* PR tree-opt/109938 */
+
+#include "../../gcc.dg/tree-ssa/pr109938.c"
+
+int 
+main ()
+{
+  if (t1 (29789, 29477, 23942) != 30045) __builtin_abort ();
+  if (t2 (-20196, 18743, -32901) != -1729) __builtin_abort ();
+  if (t3 (2136614690L, 1136698390L, 2123767997L) != 2145003318UL) __builtin_abort ();
+  if (t4 (-4878, 9977, 23313) != 61171) __builtin_abort ();
+  if (t5 (127, 99, 43) != 127) __builtin_abort ();
+  if (t6 (9176690219839792930LL, 3176690219839721234LL, 5671738468274920831LL)
+      != 9177833729112616754LL) __builtin_abort ();
+  if (t7 (29789, 29477, 23942) != 30045) __builtin_abort ();
+  if (t8 (23489, 99477, 87942) != 90053) __builtin_abort ();
+  if (t9 (10489, 66477, -73313) != 10749) __builtin_abort ();
+  if (t10 (2136614690L, -1136614690L, 4136614690UL) != 4284131106UL)
+    __builtin_abort ();
+  if (t11 (29789, 29477, 12345) != 29821) __builtin_abort ();
+  if (t12 (-120, 98, -73) != 170) __builtin_abort ();
+  if (t13 (9176690219839792930ULL, -3176690219839721234LL, 5671738468274920831ULL)
+      != 9221726284835125102ULL) __builtin_abort ();
+  v4si a1 = {29789, -20196, 23489, 10489};
+  v4si a2 = {29477, 18743, 99477, 66477}; 
+  v4si a3 = {23942, -32901, 87942, -73313};
+  v4si r1 = {30045, 63807, 90053, 10749}; 
+  v4si b1 = t14 (a1, a2, a3);
+  v4si b2 = t15 (a1, a2, a3);
+  if (__builtin_memcmp (&b1,  &r1,  sizeof (b1) != 0)) __builtin_abort();	
+  if (__builtin_memcmp (&b2,  &r1,  sizeof (b2) != 0)) __builtin_abort();
+  return 0;
+}

	Jakub


  reply	other threads:[~2023-08-10 15:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-04 21:48 [PATCH] match.pd: " Drew Ross
2023-08-08 13:18 ` Richard Biener
2023-08-10 10:28   ` Jakub Jelinek
2023-08-10 15:43     ` Jakub Jelinek [this message]
2023-08-11  7:36       ` [PATCH] match.pd, v2: " Richard Biener

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=ZNUFhqrSn3Gcq09w@tucnak \
    --to=jakub@redhat.com \
    --cc=drewross7@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /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).