From: Michael Collison <collison@rivosinc.com>
To: gcc-patches@gcc.gnu.org
Cc: Jeff Law <jlaw@ventanamicro.com>,
"jakub@redhat.com >> Jakub Jelinek" <jakub@redhat.com>
Subject: [PATCH] match.pd: rewrite select to branchless expression
Date: Tue, 8 Nov 2022 15:02:20 -0500 [thread overview]
Message-ID: <adca186b-24e1-20da-9e4d-0acb6754f133@rivosinc.com> (raw)
This patches transforms (cond (and (x , 0x1) == 0), y, (z op y)) into
(-(and (x , 0x1)) & z ) op y, where op is a '^' or a '|'. It also
transforms (cond (and (x , 0x1) != 0), (z op y), y ) into (-(and (x ,
0x1)) & z ) op y.
Matching this patterns allows GCC to generate branchless code for one of
the functions in coremark.
Bootstrapped and tested on x86 and RISC-V. Okay?
Michael.
2022-11-08 Michael Collison <collison@rivosinc.com>
* match.pd ((cond (and (x , 0x1) == 0), y, (z op y) )
-> (-(and (x , 0x1)) & z ) op y)
2022-11-08 Michael Collison <collison@rivosinc.com>
* gcc.dg/tree-ssa/branchless-cond.c: New test.
---
gcc/match.pd | 22 ++++++++++++++++
.../gcc.dg/tree-ssa/branchless-cond.c | 26 +++++++++++++++++++
2 files changed, 48 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 194ba8f5188..722f517ac6d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3486,6 +3486,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
(max @2 @1))
+/* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z )
^ y */
+(for op (bit_xor bit_ior)
+ (simplify
+ (cond (eq (bit_and @0 integer_onep@1)
+ integer_zerop)
+ @2
+ (op:c @3 @2))
+ (if (INTEGRAL_TYPE_P (type)
+ && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
+ (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2))))
+
+/* (cond (and (x , 0x1) != 0), (z ^ y), y ) -> (-(and (x , 0x1)) & z )
^ y */
+(for op (bit_xor bit_ior)
+ (simplify
+ (cond (ne (bit_and @0 integer_onep@1)
+ integer_zerop)
+ (op:c @3 @2)
+ @2)
+ (if (INTEGRAL_TYPE_P (type)
+ && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
+ (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2))))
+
/* Simplifications of shift and rotates. */
(for rotate (lrotate rrotate)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c
b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c
new file mode 100644
index 00000000000..68087ae6568
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f1(unsigned int x, unsigned int y, unsigned int z)
+{
+ return ((x & 1) == 0) ? y : z ^ y;
+}
+
+int f2(unsigned int x, unsigned int y, unsigned int z)
+{
+ return ((x & 1) != 0) ? z ^ y : y;
+}
+
+int f3(unsigned int x, unsigned int y, unsigned int z)
+{
+ return ((x & 1) == 0) ? y : z | y;
+}
+
+int f4(unsigned int x, unsigned int y, unsigned int z)
+{
+ return ((x & 1) != 0) ? z | y : y;
+}
+
+/* { dg-final { scan-tree-dump-times " -" 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " & " 8 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "if" "optimized" } } */
--
2.34.1
next reply other threads:[~2022-11-08 20:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-08 20:02 Michael Collison [this message]
2022-11-08 20:15 ` Andrew Pinski
2022-11-17 21:59 ` Jeff Law
2022-11-09 7:41 ` Richard Biener
2022-11-09 21:06 ` Michael Collison
2022-11-10 9:03 ` 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=adca186b-24e1-20da-9e4d-0acb6754f133@rivosinc.com \
--to=collison@rivosinc.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=jlaw@ventanamicro.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).