public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: Add bitwise and pattern [PR106243]
@ 2022-08-03 19:10 Sam Feifer
  2022-08-03 20:44 ` Prathamesh Kulkarni
  2022-08-04  7:06 ` Richard Biener
  0 siblings, 2 replies; 6+ messages in thread
From: Sam Feifer @ 2022-08-03 19:10 UTC (permalink / raw)
  To: GCC Patches

This patch adds a new optimization to match.pd. The pattern, -x & 1,
now gets simplified to x & 1, reducing the number of instructions
produced.

This patch also adds tests for the optimization rule.

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

	PR tree-optimization/106243

gcc/ChangeLog:

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

gcc/testsuite/ChangeLog:

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

diff --git a/gcc/match.pd b/gcc/match.pd
index 562138a8034..78b32567836 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8061,3 +8061,8 @@ and,
       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
         (bit_and @0 @1)
       (cond (le @0 @1) @0 (bit_and @0 @1))))))
+
+/* -x & 1 -> x & 1.  */
+(simplify 
+  (bit_and:c (negate @0) integer_onep@1)
+  (bit_and @0 @1))
diff --git a/gcc/testsuite/gcc.dg/pr106243-1.c b/gcc/testsuite/gcc.dg/pr106243-1.c
new file mode 100644
index 00000000000..b1dbe5cbe44
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106243-1.c
@@ -0,0 +1,18 @@
+/* PR tree-optimization/106243 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#include "pr106243.c"
+
+int main () {
+
+    if (foo(3) != 1
+        || bar(-6) != 0
+        || baz(17) != 1
+        || qux(-128) != 0
+        || foo(127) != 1) {
+            __builtin_abort();
+        }
+
+    return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/pr106243.c b/gcc/testsuite/gcc.dg/pr106243.c
new file mode 100644
index 00000000000..ee2706f2bf9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr106243.c
@@ -0,0 +1,43 @@
+/* PR tree-optimization/106243 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+#define vector __attribute__((vector_size(4*sizeof(int))))
+
+/* Test from PR.  */
+__attribute__((noipa)) int foo (int x) {
+    return -x & 1;
+}
+
+/* Other test from PR.  */
+__attribute__((noipa)) int bar (int x) {
+    return (0 - x) & 1;
+}
+
+/* Forward propogation.  */
+__attribute__((noipa)) int baz (int x) {
+    x = -x;
+    return x & 1;
+}
+
+/* Commutative property.  */
+__attribute__((noipa)) int qux (int x) {
+    return 1 & -x;
+}
+
+/* Vector test case.  */
+__attribute__((noipa)) vector int waldo (vector int x) {
+    return -x & 1;
+}
+
+/* Should not simplify.  */
+__attribute__((noipa)) int thud (int x) {
+    return -x & 2;
+}
+
+/* Should not simplify.  */
+__attribute__((noipa)) int corge (int x) {
+    return -x & -1;
+}
+
+/* { dg-final {scan-tree-dump-times "-" 2 "optimized" } } */

base-commit: 388fbbd895e72669909173c3003ae65c6483a3c2
-- 
2.31.1


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

* Re: [PATCH] match.pd: Add bitwise and pattern [PR106243]
  2022-08-03 19:10 [PATCH] match.pd: Add bitwise and pattern [PR106243] Sam Feifer
@ 2022-08-03 20:44 ` Prathamesh Kulkarni
  2022-08-03 22:39   ` Jeff Law
  2022-08-04  7:06 ` Richard Biener
  1 sibling, 1 reply; 6+ messages in thread
From: Prathamesh Kulkarni @ 2022-08-03 20:44 UTC (permalink / raw)
  To: Sam Feifer; +Cc: GCC Patches

On Thu, 4 Aug 2022 at 00:41, Sam Feifer via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch adds a new optimization to match.pd. The pattern, -x & 1,
> now gets simplified to x & 1, reducing the number of instructions
> produced.
Hi Sam,
No comments on patch, but wondering if we can similarly add another pattern to
simplify abs(x) & 1 -> x & 1 ?
Currently we don't appear to do it on GIMPLE:

__attribute__((noipa))
int f1 (int x)
{
  return __builtin_abs (x) & 1;
}

.optimized dump shows:
  _1 = ABS_EXPR <x_2(D)>;
  _3 = _1 & 1;
  return _3;

altho combine simplifies it to x & 1 on RTL, resulting in code-gen:
f1:
        and     w0, w0, 1
        ret

Thanks,
Prathamesh
>
> This patch also adds tests for the optimization rule.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
>         PR tree-optimization/106243
>
> gcc/ChangeLog:
>
>         * match.pd (-x & 1): New simplification.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/pr106243-1.c: New test.
>         * gcc.dg/pr106243.c: New test.
> ---
>  gcc/match.pd                      |  5 ++++
>  gcc/testsuite/gcc.dg/pr106243-1.c | 18 +++++++++++++
>  gcc/testsuite/gcc.dg/pr106243.c   | 43 +++++++++++++++++++++++++++++++
>  3 files changed, 66 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 562138a8034..78b32567836 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -8061,3 +8061,8 @@ and,
>        (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
>          (bit_and @0 @1)
>        (cond (le @0 @1) @0 (bit_and @0 @1))))))
> +
> +/* -x & 1 -> x & 1.  */
> +(simplify
> +  (bit_and:c (negate @0) integer_onep@1)
> +  (bit_and @0 @1))
> diff --git a/gcc/testsuite/gcc.dg/pr106243-1.c b/gcc/testsuite/gcc.dg/pr106243-1.c
> new file mode 100644
> index 00000000000..b1dbe5cbe44
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243-1.c
> @@ -0,0 +1,18 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +#include "pr106243.c"
> +
> +int main () {
> +
> +    if (foo(3) != 1
> +        || bar(-6) != 0
> +        || baz(17) != 1
> +        || qux(-128) != 0
> +        || foo(127) != 1) {
> +            __builtin_abort();
> +        }
> +
> +    return 0;
> +}
> diff --git a/gcc/testsuite/gcc.dg/pr106243.c b/gcc/testsuite/gcc.dg/pr106243.c
> new file mode 100644
> index 00000000000..ee2706f2bf9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243.c
> @@ -0,0 +1,43 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +#define vector __attribute__((vector_size(4*sizeof(int))))
> +
> +/* Test from PR.  */
> +__attribute__((noipa)) int foo (int x) {
> +    return -x & 1;
> +}
> +
> +/* Other test from PR.  */
> +__attribute__((noipa)) int bar (int x) {
> +    return (0 - x) & 1;
> +}
> +
> +/* Forward propogation.  */
> +__attribute__((noipa)) int baz (int x) {
> +    x = -x;
> +    return x & 1;
> +}
> +
> +/* Commutative property.  */
> +__attribute__((noipa)) int qux (int x) {
> +    return 1 & -x;
> +}
> +
> +/* Vector test case.  */
> +__attribute__((noipa)) vector int waldo (vector int x) {
> +    return -x & 1;
> +}
> +
> +/* Should not simplify.  */
> +__attribute__((noipa)) int thud (int x) {
> +    return -x & 2;
> +}
> +
> +/* Should not simplify.  */
> +__attribute__((noipa)) int corge (int x) {
> +    return -x & -1;
> +}
> +
> +/* { dg-final {scan-tree-dump-times "-" 2 "optimized" } } */
>
> base-commit: 388fbbd895e72669909173c3003ae65c6483a3c2
> --
> 2.31.1
>

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

* Re: [PATCH] match.pd: Add bitwise and pattern [PR106243]
  2022-08-03 20:44 ` Prathamesh Kulkarni
@ 2022-08-03 22:39   ` Jeff Law
  2022-08-04  7:08     ` Richard Biener
  2022-08-04 12:34     ` Michael Matz
  0 siblings, 2 replies; 6+ messages in thread
From: Jeff Law @ 2022-08-03 22:39 UTC (permalink / raw)
  To: gcc-patches



On 8/3/2022 2:44 PM, Prathamesh Kulkarni via Gcc-patches wrote:
> On Thu, 4 Aug 2022 at 00:41, Sam Feifer via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>> This patch adds a new optimization to match.pd. The pattern, -x & 1,
>> now gets simplified to x & 1, reducing the number of instructions
>> produced.
> Hi Sam,
> No comments on patch, but wondering if we can similarly add another pattern to
> simplify abs(x) & 1 -> x & 1 ?
> Currently we don't appear to do it on GIMPLE:
>
> __attribute__((noipa))
> int f1 (int x)
> {
>    return __builtin_abs (x) & 1;
> }
>
> .optimized dump shows:
>    _1 = ABS_EXPR <x_2(D)>;
>    _3 = _1 & 1;
>    return _3;
>
> altho combine simplifies it to x & 1 on RTL, resulting in code-gen:
> f1:
>          and     w0, w0, 1
>          ret
Doesn't the abs(x) & mask simplify to x & mask for any mask where the 
sign bit of x is off -- including cases where mask isn't necessarily a 
compile-time constant, but we have range data which allows us to know 
that x's sign bit is off in mask.

jeff




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

* Re: [PATCH] match.pd: Add bitwise and pattern [PR106243]
  2022-08-03 19:10 [PATCH] match.pd: Add bitwise and pattern [PR106243] Sam Feifer
  2022-08-03 20:44 ` Prathamesh Kulkarni
@ 2022-08-04  7:06 ` Richard Biener
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Biener @ 2022-08-04  7:06 UTC (permalink / raw)
  To: Sam Feifer; +Cc: GCC Patches

On Wed, Aug 3, 2022 at 9:11 PM Sam Feifer via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch adds a new optimization to match.pd. The pattern, -x & 1,
> now gets simplified to x & 1, reducing the number of instructions
> produced.
>
> This patch also adds tests for the optimization rule.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
>         PR tree-optimization/106243
>
> gcc/ChangeLog:
>
>         * match.pd (-x & 1): New simplification.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/pr106243-1.c: New test.
>         * gcc.dg/pr106243.c: New test.
> ---
>  gcc/match.pd                      |  5 ++++
>  gcc/testsuite/gcc.dg/pr106243-1.c | 18 +++++++++++++
>  gcc/testsuite/gcc.dg/pr106243.c   | 43 +++++++++++++++++++++++++++++++
>  3 files changed, 66 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/pr106243.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 562138a8034..78b32567836 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -8061,3 +8061,8 @@ and,
>        (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
>          (bit_and @0 @1)
>        (cond (le @0 @1) @0 (bit_and @0 @1))))))
> +
> +/* -x & 1 -> x & 1.  */
> +(simplify
> +  (bit_and:c (negate @0) integer_onep@1)

Note the bit_and doesn't need :c because constant operands are always
canonicalized second.

OK with that change.
Thanks,
Richard.

> +  (bit_and @0 @1))
> diff --git a/gcc/testsuite/gcc.dg/pr106243-1.c b/gcc/testsuite/gcc.dg/pr106243-1.c
> new file mode 100644
> index 00000000000..b1dbe5cbe44
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243-1.c
> @@ -0,0 +1,18 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do run } */
> +/* { dg-options "-O2" } */
> +
> +#include "pr106243.c"
> +
> +int main () {
> +
> +    if (foo(3) != 1
> +        || bar(-6) != 0
> +        || baz(17) != 1
> +        || qux(-128) != 0
> +        || foo(127) != 1) {
> +            __builtin_abort();
> +        }
> +
> +    return 0;
> +}
> diff --git a/gcc/testsuite/gcc.dg/pr106243.c b/gcc/testsuite/gcc.dg/pr106243.c
> new file mode 100644
> index 00000000000..ee2706f2bf9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr106243.c
> @@ -0,0 +1,43 @@
> +/* PR tree-optimization/106243 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +#define vector __attribute__((vector_size(4*sizeof(int))))
> +
> +/* Test from PR.  */
> +__attribute__((noipa)) int foo (int x) {
> +    return -x & 1;
> +}
> +
> +/* Other test from PR.  */
> +__attribute__((noipa)) int bar (int x) {
> +    return (0 - x) & 1;
> +}
> +
> +/* Forward propogation.  */
> +__attribute__((noipa)) int baz (int x) {
> +    x = -x;
> +    return x & 1;
> +}
> +
> +/* Commutative property.  */
> +__attribute__((noipa)) int qux (int x) {
> +    return 1 & -x;
> +}
> +
> +/* Vector test case.  */
> +__attribute__((noipa)) vector int waldo (vector int x) {
> +    return -x & 1;
> +}
> +
> +/* Should not simplify.  */
> +__attribute__((noipa)) int thud (int x) {
> +    return -x & 2;
> +}
> +
> +/* Should not simplify.  */
> +__attribute__((noipa)) int corge (int x) {
> +    return -x & -1;
> +}
> +
> +/* { dg-final {scan-tree-dump-times "-" 2 "optimized" } } */
>
> base-commit: 388fbbd895e72669909173c3003ae65c6483a3c2
> --
> 2.31.1
>

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

* Re: [PATCH] match.pd: Add bitwise and pattern [PR106243]
  2022-08-03 22:39   ` Jeff Law
@ 2022-08-04  7:08     ` Richard Biener
  2022-08-04 12:34     ` Michael Matz
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Biener @ 2022-08-04  7:08 UTC (permalink / raw)
  To: Jeff Law; +Cc: GCC Patches

On Thu, Aug 4, 2022 at 12:40 AM Jeff Law via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
>
>
> On 8/3/2022 2:44 PM, Prathamesh Kulkarni via Gcc-patches wrote:
> > On Thu, 4 Aug 2022 at 00:41, Sam Feifer via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> >> This patch adds a new optimization to match.pd. The pattern, -x & 1,
> >> now gets simplified to x & 1, reducing the number of instructions
> >> produced.
> > Hi Sam,
> > No comments on patch, but wondering if we can similarly add another pattern to
> > simplify abs(x) & 1 -> x & 1 ?
> > Currently we don't appear to do it on GIMPLE:
> >
> > __attribute__((noipa))
> > int f1 (int x)
> > {
> >    return __builtin_abs (x) & 1;
> > }
> >
> > .optimized dump shows:
> >    _1 = ABS_EXPR <x_2(D)>;
> >    _3 = _1 & 1;
> >    return _3;
> >
> > altho combine simplifies it to x & 1 on RTL, resulting in code-gen:
> > f1:
> >          and     w0, w0, 1
> >          ret
> Doesn't the abs(x) & mask simplify to x & mask for any mask where the
> sign bit of x is off -- including cases where mask isn't necessarily a
> compile-time constant, but we have range data which allows us to know
> that x's sign bit is off in mask.

You can use tree_expr_nonnegative_p but then that does simplify
abs(x) to x already.  But sure, handling abs() like negate sounds it
should work.

Richard,

> jeff
>
>
>

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

* Re: [PATCH] match.pd: Add bitwise and pattern [PR106243]
  2022-08-03 22:39   ` Jeff Law
  2022-08-04  7:08     ` Richard Biener
@ 2022-08-04 12:34     ` Michael Matz
  1 sibling, 0 replies; 6+ messages in thread
From: Michael Matz @ 2022-08-04 12:34 UTC (permalink / raw)
  To: Jeff Law; +Cc: gcc-patches

Hello,

On Wed, 3 Aug 2022, Jeff Law via Gcc-patches wrote:

> > .optimized dump shows:
> >    _1 = ABS_EXPR <x_2(D)>;
> >    _3 = _1 & 1;
> >    return _3;
> > 
> > altho combine simplifies it to x & 1 on RTL, resulting in code-gen:
> > f1:
> >          and     w0, w0, 1
> >          ret
> Doesn't the abs(x) & mask simplify to x & mask for any mask where the sign bit
> of x is off

No.  Only the lowest bit remains the same between x and -x, all others 
might or might not be inverted (first counter example: x=-3, mask=3).


Ciao,
Michael.

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

end of thread, other threads:[~2022-08-04 12:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-03 19:10 [PATCH] match.pd: Add bitwise and pattern [PR106243] Sam Feifer
2022-08-03 20:44 ` Prathamesh Kulkarni
2022-08-03 22:39   ` Jeff Law
2022-08-04  7:08     ` Richard Biener
2022-08-04 12:34     ` Michael Matz
2022-08-04  7:06 ` 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).