public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH take 2] Fold bswap32(x) != 0 to x != 0 (and related transforms)
@ 2021-07-24  9:44 Roger Sayle
  2021-07-26 14:55 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2021-07-24  9:44 UTC (permalink / raw)
  To: 'GCC Patches'; +Cc: 'Marc Glisse', 'Richard Biener'

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


My apologies for the short delay.  Thanks for explaining why these
transforms
don't usually require explicit tests for side-effects (but occasionally do).
This does simplify things; please find attached the shorter revised patch.

This patch has been retested on x86_64-pc-linux-gnu with a make bootstrap
and make -k check with no new failures.  Ok for mainline?

2010-07-24  Roger Sayle  <roger@nextmovesoftware.com>
	    Marc Glisse  <marc.glisse@inria.fr>

gcc/ChangeLog
	* match.pd (rotate): Simplify equality/inequality of rotations.
	(bswap): Simplify equality/inequality tests of byte swapping.

gcc/testsuite/ChangeLog
	* gcc.dg/fold-eqrotate-1.c: New test case.
	* gcc.dg/fold-eqbswap-1.c: New test case.

Roger
--

-----Original Message-----
From: Marc Glisse <marc.glisse@inria.fr> 
Sent: 18 July 2021 23:03
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: 'GCC Patches' <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Fold bswap32(x) != 0 to x != 0 (and related transforms)

On Sun, 18 Jul 2021, Roger Sayle wrote:

> +    (if (GIMPLE || !TREE_SIDE_EFFECTS (@0))

I don't think you need to worry about that, the general genmatch machinery
is already supposed to take care of it. All the existing cases in match.pd
are about cond_expr, where counting the occurrences of each @i is not
reliable.

--
Marc Glisse

[-- Attachment #2: patchc2.txt --]
[-- Type: text/plain, Size: 1466 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index beb8d27..4d41b70 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3312,6 +3312,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
      { tree rotate_type = TREE_TYPE (@0); }
       (convert (rotate (convert:rotate_type @1) @2))))))
 
+(for cmp (eq ne)
+ (for rotate (lrotate rrotate)
+      invrot (rrotate lrotate)
+  /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
+  (simplify
+   (cmp (rotate @1 @0) (rotate @2 @0))
+   (cmp @1 @2))
+  /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
+  (simplify
+   (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
+   (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
+  /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
+  (simplify
+   (cmp (rotate @0 @1) INTEGER_CST@2)
+    (if (integer_zerop (@2) || integer_all_onesp (@2))
+     (cmp @0 @2)))))
+
 /* Simplifications of conversions.  */
 
 /* Basic strip-useless-type-conversions / strip_nops.  */
@@ -3622,6 +3639,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (simplify
    (bswap (bitop:c (bswap @0) @1))
    (bitop @0 (bswap @1))))
+ (for cmp (eq ne)
+  (simplify
+   (cmp (bswap @0) (bswap @1))
+   (cmp @0 @1))
+  (simplify
+   (cmp (bswap @0) INTEGER_CST@1)
+   (cmp @0 (bswap @1))))
  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
  (simplify
   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))

[-- Attachment #3: fold-eqbswap-1.c --]
[-- Type: text/plain, Size: 1870 bytes --]

/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */

int test1(int x, int y)
{
#if __SIZEOF_INT__ == 4
  return __builtin_bswap32(x) == __builtin_bswap32(y);
#else
  return x == y;
#endif
}

int test2(int x, int y)
{
#if __SIZEOF_INT__ == 4
  return __builtin_bswap32(x) != __builtin_bswap32(y);
#else
  return x != y;
#endif
}

int test3(int x)
{
#if __SIZEOF_INT__ == 4
  return __builtin_bswap32(x) == 12345;
#else
  return x;
#endif
}

int test4(int x)
{
#if __SIZEOF_INT__ == 4
  return __builtin_bswap32(x) != 12345;
#else
  return x;
#endif
}

int test1ll(long long x, long long y)
{
#if __SIZEOF_LONG_LONG__ == 8
  return __builtin_bswap64(x) == __builtin_bswap64(y);
#else
  return x == y;
#endif
}

int test2ll(long long x, long long y)
{
#if __SIZEOF_LONG_LONG__ == 8
  return __builtin_bswap64(x) != __builtin_bswap64(y);
#else
  return x != y;
#endif
}

int test3ll(long long x)
{
#if __SIZEOF_LONG_LONG__ == 8
  return __builtin_bswap64(x) == 12345;
#else
  return (int)x;
#endif
}

int test4ll(long long x)
{
#if __SIZEOF_LONG_LONG__ == 8
  return __builtin_bswap64(x) != 12345;
#else
  return (int)x;
#endif
}

int test1s(short x, short y)
{
#if __SIZEOF_SHORT__ == 2
  return __builtin_bswap16(x) == __builtin_bswap16(y);
#else
  return x == y;
#endif
}

int test2s(short x, short y)
{
#if __SIZEOF_SHORT__ == 2
  return __builtin_bswap16(x) != __builtin_bswap16(y);
#else
  return x != y;
#endif
}

int test3s(short x)
{
#if __SIZEOF_SHORT__ == 2
  return __builtin_bswap16(x) == 12345;
#else
  return (int)x;
#endif
}

int test4s(short x)
{
#if __SIZEOF_SHORT__ == 2
  return __builtin_bswap16(x) != 12345;
#else
  return (int)x;
#endif
}

/* { dg-final { scan-tree-dump-times "__builtin_bswap" 0 "optimized" } } */


[-- Attachment #4: fold-eqrotate-1.c --]
[-- Type: text/plain, Size: 839 bytes --]

/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-optimized" } */

int test1(unsigned int x, unsigned int y)
{
#if __SIZEOF_INT__ == 4
  unsigned int r1 = (x << 16) | (x >> 16);
  unsigned int r2 = (y << 16) | (y >> 16);
  return r1 == r2;
#else
  return x == y;
#endif
}

int test2(unsigned int x)
{
#if __SIZEOF_INT__ == 4
  unsigned int r1 = (x << 16) | (x >> 16);
  return r1 == 12345;
#else
  return x == 12345;
#endif
}

int test3(unsigned int x)
{
#if __SIZEOF_INT__ == 4
  unsigned int r1 = (x << 16) | (x >> 16);
  return r1 == 0;
#else
  return x == 0;
#endif
}

int test4(unsigned int x)
{
#if __SIZEOF_INT__ == 4
  unsigned int r1 = (x << 16) | (x >> 16);
  return r1 == ~0;
#else
  return x == ~0;
#endif
}

/* { dg-final { scan-tree-dump-times "r>>" 0 "optimized" } } */


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH take 2] Fold bswap32(x) != 0 to x != 0 (and related transforms)
  2021-07-24  9:44 [PATCH take 2] Fold bswap32(x) != 0 to x != 0 (and related transforms) Roger Sayle
@ 2021-07-26 14:55 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2021-07-26 14:55 UTC (permalink / raw)
  To: Roger Sayle, 'GCC Patches'; +Cc: 'Marc Glisse'



On 7/24/2021 3:44 AM, Roger Sayle wrote:
> My apologies for the short delay.  Thanks for explaining why these
> transforms
> don't usually require explicit tests for side-effects (but occasionally do).
> This does simplify things; please find attached the shorter revised patch.
>
> This patch has been retested on x86_64-pc-linux-gnu with a make bootstrap
> and make -k check with no new failures.  Ok for mainline?
>
> 2010-07-24  Roger Sayle  <roger@nextmovesoftware.com>
> 	    Marc Glisse  <marc.glisse@inria.fr>
>
> gcc/ChangeLog
> 	* match.pd (rotate): Simplify equality/inequality of rotations.
> 	(bswap): Simplify equality/inequality tests of byte swapping.
>
> gcc/testsuite/ChangeLog
> 	* gcc.dg/fold-eqrotate-1.c: New test case.
> 	* gcc.dg/fold-eqbswap-1.c: New test case.
OK.  Thanks!

jeff


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-26 14:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-24  9:44 [PATCH take 2] Fold bswap32(x) != 0 to x != 0 (and related transforms) Roger Sayle
2021-07-26 14:55 ` Jeff Law

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).