public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: Add abs with bitwise and pattern [PR106243]
@ 2022-08-10 17:10 Sam Feifer
  2022-08-26  8:38 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Sam Feifer @ 2022-08-10 17:10 UTC (permalink / raw)
  To: GCC Patches

This patch adds a simplification to match.pd that was discussed on the
thread for pr106243. It simplifies the pattern, abs(x) & 1, to x & 1.

There are also tests for the simplification in this patch. I couldn't
figure out how to get abs to work with vectors. If a test for that is
necessary, could I get some guidance on using abs with vector types?

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR tree-optimization/106243

gcc/ChangeLog:

	* match.pd (abs(x) & 1): New simplification.

gcc/testsuite/ChangeLog:

	* gcc.dg/pr106243-2.c: New test.
	* gcc.dg/pr106243-3.c: New test.
---
 gcc/match.pd                      |  5 +++++
 gcc/testsuite/gcc.dg/pr106243-2.c | 31 +++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/pr106243-3.c | 18 ++++++++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr106243-2.c
 create mode 100644 gcc/testsuite/gcc.dg/pr106243-3.c

diff --git a/gcc/match.pd b/gcc/match.pd
index f82f94ad1fe..c04e70f34c1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8071,3 +8071,8 @@ and,
 (simplify 
   (bit_and (negate @0) integer_onep@1)
   (bit_and @0 @1))
+
+/* abs(x) & 1 -> x & 1.  */
+(simplify
+  (bit_and (abs @0) integer_onep@1)
+  (bit_and @0 @1))
diff --git a/gcc/testsuite/gcc.dg/pr106243-2.c b/gcc/testsuite/gcc.dg/pr106243-2.c
new file mode 100644
index 00000000000..27e66f59160
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106243-2.c
@@ -0,0 +1,31 @@
+/* PR tree-optimization/106243 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#include <stdlib.h>
+
+__attribute__((noipa)) int foo (int x) {
+    return abs(x) & 1; 
+}
+
+__attribute__((noipa)) int bar (int x) {
+    return (0 - abs(x)) & 1;
+}
+
+/* Commutative property.  */
+__attribute__((noipa)) int baz (int x) {
+    return 1 & abs(x);
+}
+
+/* Forward propogation.  */
+__attribute__((noipa)) int qux (int x) {
+    int y = abs(x);
+    return y & 1;
+}
+
+/* Should not simplify.  */
+__attribute__((noipa)) int thud (int x) {
+    return abs(x) & -1;
+}
+
+/* { dg-final {scan-tree-dump-times " ABS_EXPR " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr106243-3.c b/gcc/testsuite/gcc.dg/pr106243-3.c
new file mode 100644
index 00000000000..68800868751
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106243-3.c
@@ -0,0 +1,18 @@
+/* PR tree-optimization/106243 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#include "pr106243-2.c"
+
+int main () {
+
+    if (foo(6) != 0
+        || bar(-3) != 1
+        || baz(32) != 0
+        || qux(-128) != 0
+        || foo (127) != 1) {
+            __builtin_abort();
+        }
+    
+    return 0;
+}

base-commit: be58bf98e98bb431ed26ca8be84586075fe8be82
-- 
2.31.1


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

* Re: [PATCH] match.pd: Add abs with bitwise and pattern [PR106243]
  2022-08-10 17:10 [PATCH] match.pd: Add abs with bitwise and pattern [PR106243] Sam Feifer
@ 2022-08-26  8:38 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-08-26  8:38 UTC (permalink / raw)
  To: Sam Feifer; +Cc: GCC Patches

On Wed, Aug 10, 2022 at 7:11 PM Sam Feifer via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch adds a simplification to match.pd that was discussed on the
> thread for pr106243. It simplifies the pattern, abs(x) & 1, to x & 1.
>
> There are also tests for the simplification in this patch. I couldn't
> figure out how to get abs to work with vectors. If a test for that is
> necessary, could I get some guidance on using abs with vector types?
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK, and sorry for the delay.

Thanks,
Richard.

>         PR tree-optimization/106243
>
> gcc/ChangeLog:
>
>         * match.pd (abs(x) & 1): New simplification.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/pr106243-2.c: New test.
>         * gcc.dg/pr106243-3.c: New test.
> ---
>  gcc/match.pd                      |  5 +++++
>  gcc/testsuite/gcc.dg/pr106243-2.c | 31 +++++++++++++++++++++++++++++++
>  gcc/testsuite/gcc.dg/pr106243-3.c | 18 ++++++++++++++++++
>  3 files changed, 54 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243-2.c
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243-3.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index f82f94ad1fe..c04e70f34c1 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -8071,3 +8071,8 @@ and,
>  (simplify
>    (bit_and (negate @0) integer_onep@1)
>    (bit_and @0 @1))
> +
> +/* abs(x) & 1 -> x & 1.  */
> +(simplify
> +  (bit_and (abs @0) integer_onep@1)
> +  (bit_and @0 @1))
> diff --git a/gcc/testsuite/gcc.dg/pr106243-2.c b/gcc/testsuite/gcc.dg/pr106243-2.c
> new file mode 100644
> index 00000000000..27e66f59160
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243-2.c
> @@ -0,0 +1,31 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +#include <stdlib.h>
> +
> +__attribute__((noipa)) int foo (int x) {
> +    return abs(x) & 1;
> +}
> +
> +__attribute__((noipa)) int bar (int x) {
> +    return (0 - abs(x)) & 1;
> +}
> +
> +/* Commutative property.  */
> +__attribute__((noipa)) int baz (int x) {
> +    return 1 & abs(x);
> +}
> +
> +/* Forward propogation.  */
> +__attribute__((noipa)) int qux (int x) {
> +    int y = abs(x);
> +    return y & 1;
> +}
> +
> +/* Should not simplify.  */
> +__attribute__((noipa)) int thud (int x) {
> +    return abs(x) & -1;
> +}
> +
> +/* { dg-final {scan-tree-dump-times " ABS_EXPR " 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/pr106243-3.c b/gcc/testsuite/gcc.dg/pr106243-3.c
> new file mode 100644
> index 00000000000..68800868751
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243-3.c
> @@ -0,0 +1,18 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +#include "pr106243-2.c"
> +
> +int main () {
> +
> +    if (foo(6) != 0
> +        || bar(-3) != 1
> +        || baz(32) != 0
> +        || qux(-128) != 0
> +        || foo (127) != 1) {
> +            __builtin_abort();
> +        }
> +
> +    return 0;
> +}
>
> base-commit: be58bf98e98bb431ed26ca8be84586075fe8be82
> --
> 2.31.1
>

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

end of thread, other threads:[~2022-08-26  8:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10 17:10 [PATCH] match.pd: Add abs with bitwise and pattern [PR106243] Sam Feifer
2022-08-26  8:38 ` Richard Biener

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