From: "Roger Sayle" <roger@nextmovesoftware.com>
To: "'GCC Patches'" <gcc-patches@gcc.gnu.org>
Cc: "'Marc Glisse'" <marc.glisse@inria.fr>,
"'Richard Biener'" <richard.guenther@gmail.com>
Subject: [PATCH take 2] Fold bswap32(x) != 0 to x != 0 (and related transforms)
Date: Sat, 24 Jul 2021 10:44:04 +0100 [thread overview]
Message-ID: <087b01d78070$7299bf10$57cd3d30$@nextmovesoftware.com> (raw)
[-- 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" } } */
next reply other threads:[~2021-07-24 9:44 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-24 9:44 Roger Sayle [this message]
2021-07-26 14:55 ` Jeff Law
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='087b01d78070$7299bf10$57cd3d30$@nextmovesoftware.com' \
--to=roger@nextmovesoftware.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=marc.glisse@inria.fr \
--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).