public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1"
@ 2023-02-01  1:29 Andrew Pinski
  2023-02-01  7:58 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-02-01  1:29 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

For bool values, it is easier to deal with
xor 1 rather than having 1 - a. This is because
we are more likely to simplify the xor further in many
cases.

This is a special case for (MASK - b) where MASK
is a powerof2 - 1 and b <= MASK but only for bool
ranges ([0,1]) as that is the main case where the
difference comes into play.

Note this is enabled for gimple folding only
as the ranges are only know while doing gimple
folding and cfun is not always set when fold is called.

OK? Bootstrapped and tested on x86_64-linux-gnu with no
regressions.

gcc/ChangeLog:

	PR tree-optimization/108355
	PR tree-optimization/96921
	* match.pd: Add pattern for "1 - bool_val".

gcc/testsuite/ChangeLog:

	PR tree-optimization/108355
	PR tree-optimization/96921
	* gcc.dg/tree-ssa/bool-minus-1.c: New test.
	* gcc.dg/tree-ssa/bool-minus-2.c: New test.
	* gcc.dg/tree-ssa/pr108354-1.c: New test.
---
 gcc/match.pd                                 | 13 ++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c | 11 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c | 33 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c   | 26 +++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index f605b798c44..c9e8bebede2 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1732,6 +1732,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (!FIXED_POINT_TYPE_P (type))
  (plus @0 (negate @1))))
 
+#if GIMPLE
+/* 1 - a is a ^ 1 if a had a bool range. */
+/* This is only enabled for gimple as sometimes
+   cfun is not set for the function which contains
+   the SSA_NAME (e.g. while IPA passes are happening,
+   fold might be called).  */
+(simplify
+ (minus integer_onep@0 SSA_NAME@1)
+  (if (INTEGRAL_TYPE_P (type)
+       && ssa_name_has_boolean_range (@1))
+   (bit_xor @1 @0)))
+#endif
+
 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
 (simplify
  (negate (mult:c@0 @1 negate_expr_p@2))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
new file mode 100644
index 00000000000..e434ff9507a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
@@ -0,0 +1,11 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a)
+{
+  int c = 1 - a;
+  return c;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 1 "optimized" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
new file mode 100644
index 00000000000..b77d36c1d3c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
@@ -0,0 +1,33 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a, _Bool b)
+{
+  int c = 1 - a;
+  int d = 1 - b;
+  int e = c & d;
+  return 1 - e;
+}
+
+_Bool
+bar (_Bool a, _Bool b)
+{
+  int c = 1 - a;
+  int d = 1 - b;
+  _Bool e = c & d;
+  return 1 - e;
+}
+
+_Bool
+baz (_Bool a, _Bool b)
+{
+  _Bool c = 1 - a;
+  _Bool d = 1 - b;
+  _Bool e = c & d;
+  return 1 - e;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~b" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." 3 "optimized" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
new file mode 100644
index 00000000000..60d1dbc281e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
@@ -0,0 +1,26 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int b;
+int *c;
+int e;
+static int *f = &e;
+int g;
+void foo();
+short(a)(short h, short i) { return h - i; }
+int(d)(int h) { return h == 83647 ? 0 : -h; }
+int main() {
+  short j;
+  int *k = &e, *l = &b;
+  *f = 0 == c;
+  j = a(0 != 2, *k);
+  if (d(j ^ (0 == l || *k)) != *k)
+    ;
+  else
+    foo();
+  c = &g;
+}
+
+/* { dg-final { scan-tree-dump-times " 1 - " 0 "optimized" } } */
+/* There should be no calls to foo. */
+/* { dg-final { scan-tree-dump-times "foo " 0 "optimized" } } */
+
-- 
2.17.1


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

* Re: [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1"
  2023-02-01  1:29 [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1" Andrew Pinski
@ 2023-02-01  7:58 ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-02-01  7:58 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Wed, Feb 1, 2023 at 2:30 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> For bool values, it is easier to deal with
> xor 1 rather than having 1 - a. This is because
> we are more likely to simplify the xor further in many
> cases.
>
> This is a special case for (MASK - b) where MASK
> is a powerof2 - 1 and b <= MASK but only for bool
> ranges ([0,1]) as that is the main case where the
> difference comes into play.
>
> Note this is enabled for gimple folding only
> as the ranges are only know while doing gimple
> folding and cfun is not always set when fold is called.

Can we robustify ssa_name_has_boolean_range_p instead?
I see it's called even from GENERIC folding in match.pd already.

Otherwise OK.

Thanks,
Richard.

>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no
> regressions.
>
> gcc/ChangeLog:
>
>         PR tree-optimization/108355

I think this should be 108354?

>         PR tree-optimization/96921
>         * match.pd: Add pattern for "1 - bool_val".
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/108355
>         PR tree-optimization/96921
>         * gcc.dg/tree-ssa/bool-minus-1.c: New test.
>         * gcc.dg/tree-ssa/bool-minus-2.c: New test.
>         * gcc.dg/tree-ssa/pr108354-1.c: New test.
> ---
>  gcc/match.pd                                 | 13 ++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c | 11 +++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c | 33 ++++++++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c   | 26 +++++++++++++++
>  4 files changed, 83 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index f605b798c44..c9e8bebede2 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1732,6 +1732,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (!FIXED_POINT_TYPE_P (type))
>   (plus @0 (negate @1))))
>
> +#if GIMPLE
> +/* 1 - a is a ^ 1 if a had a bool range. */
> +/* This is only enabled for gimple as sometimes
> +   cfun is not set for the function which contains
> +   the SSA_NAME (e.g. while IPA passes are happening,
> +   fold might be called).  */
> +(simplify
> + (minus integer_onep@0 SSA_NAME@1)
> +  (if (INTEGRAL_TYPE_P (type)
> +       && ssa_name_has_boolean_range (@1))
> +   (bit_xor @1 @0)))
> +#endif
> +
>  /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
>  (simplify
>   (negate (mult:c@0 @1 negate_expr_p@2))
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
> new file mode 100644
> index 00000000000..e434ff9507a
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
> @@ -0,0 +1,11 @@
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +_Bool
> +foo (_Bool a)
> +{
> +  int c = 1 - a;
> +  return c;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized" } } */
> +
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
> new file mode 100644
> index 00000000000..b77d36c1d3c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
> @@ -0,0 +1,33 @@
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +_Bool
> +foo (_Bool a, _Bool b)
> +{
> +  int c = 1 - a;
> +  int d = 1 - b;
> +  int e = c & d;
> +  return 1 - e;
> +}
> +
> +_Bool
> +bar (_Bool a, _Bool b)
> +{
> +  int c = 1 - a;
> +  int d = 1 - b;
> +  _Bool e = c & d;
> +  return 1 - e;
> +}
> +
> +_Bool
> +baz (_Bool a, _Bool b)
> +{
> +  _Bool c = 1 - a;
> +  _Bool d = 1 - b;
> +  _Bool e = c & d;
> +  return 1 - e;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "~a" 0 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "~b" 0 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." 3 "optimized" } } */
> +
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
> new file mode 100644
> index 00000000000..60d1dbc281e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
> @@ -0,0 +1,26 @@
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +int b;
> +int *c;
> +int e;
> +static int *f = &e;
> +int g;
> +void foo();
> +short(a)(short h, short i) { return h - i; }
> +int(d)(int h) { return h == 83647 ? 0 : -h; }
> +int main() {
> +  short j;
> +  int *k = &e, *l = &b;
> +  *f = 0 == c;
> +  j = a(0 != 2, *k);
> +  if (d(j ^ (0 == l || *k)) != *k)
> +    ;
> +  else
> +    foo();
> +  c = &g;
> +}
> +
> +/* { dg-final { scan-tree-dump-times " 1 - " 0 "optimized" } } */
> +/* There should be no calls to foo. */
> +/* { dg-final { scan-tree-dump-times "foo " 0 "optimized" } } */
> +
> --
> 2.17.1
>

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

* Re: [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1"
  2023-02-01  1:21 Andrew Pinski
@ 2023-02-13  6:41 ` Jeff Law
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Law @ 2023-02-13  6:41 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



On 1/31/23 18:21, Andrew Pinski via Gcc-patches wrote:
> For bool values, it is easier to deal with
> xor 1 rather than having 1 - a. This is because
> we are more likely to simplify the xor further in many
> cases.
> 
> This is a special case for (MASK - b) where MASK
> is a powerof2 - 1 and b <= MASK but only for bool
> ranges ([0,1]) as that is the main case where the
> difference comes into play.
> 
> Note this is enabled for gimple folding only
> as the ranges are only know while doing gimple
> folding and cfun is not always set when fold is called.
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu with no
> regressions.
> 
> gcc/ChangeLog:
> 
> 	PR tree-optimization/108355
> 	PR tree-optimization/96921
> 	* match.pd: Add pattern for "1 - bool_val".
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR tree-optimization/108355
> 	PR tree-optimization/96921
> 	* gcc.dg/tree-ssa/bool-minus-1.c: New test.
> 	* gcc.dg/tree-ssa/bool-minus-2.c: New test.
> 	* gcc.dg/tree-ssa/pr108354-1.c: New test.
OK.
jeff

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

* [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1"
@ 2023-02-01  1:21 Andrew Pinski
  2023-02-13  6:41 ` Jeff Law
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-02-01  1:21 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

For bool values, it is easier to deal with
xor 1 rather than having 1 - a. This is because
we are more likely to simplify the xor further in many
cases.

This is a special case for (MASK - b) where MASK
is a powerof2 - 1 and b <= MASK but only for bool
ranges ([0,1]) as that is the main case where the
difference comes into play.

Note this is enabled for gimple folding only
as the ranges are only know while doing gimple
folding and cfun is not always set when fold is called.

OK? Bootstrapped and tested on x86_64-linux-gnu with no
regressions.

gcc/ChangeLog:

	PR tree-optimization/108355
	PR tree-optimization/96921
	* match.pd: Add pattern for "1 - bool_val".

gcc/testsuite/ChangeLog:

	PR tree-optimization/108355
	PR tree-optimization/96921
	* gcc.dg/tree-ssa/bool-minus-1.c: New test.
	* gcc.dg/tree-ssa/bool-minus-2.c: New test.
	* gcc.dg/tree-ssa/pr108354-1.c: New test.
---
 gcc/match.pd                                 | 13 ++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c | 11 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c | 33 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c   | 26 +++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index f605b798c44..c9e8bebede2 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1732,6 +1732,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (!FIXED_POINT_TYPE_P (type))
  (plus @0 (negate @1))))
 
+#if GIMPLE
+/* 1 - a is a ^ 1 if a had a bool range. */
+/* This is only enabled for gimple as sometimes
+   cfun is not set for the function which contains
+   the SSA_NAME (e.g. while IPA passes are happening,
+   fold might be called).  */
+(simplify
+ (minus integer_onep@0 SSA_NAME@1)
+  (if (INTEGRAL_TYPE_P (type)
+       && ssa_name_has_boolean_range (@1))
+   (bit_xor @1 @0)))
+#endif
+
 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
 (simplify
  (negate (mult:c@0 @1 negate_expr_p@2))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
new file mode 100644
index 00000000000..e434ff9507a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-1.c
@@ -0,0 +1,11 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a)
+{
+  int c = 1 - a;
+  return c;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 1 "optimized" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
new file mode 100644
index 00000000000..b77d36c1d3c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-minus-2.c
@@ -0,0 +1,33 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+_Bool
+foo (_Bool a, _Bool b)
+{
+  int c = 1 - a;
+  int d = 1 - b;
+  int e = c & d;
+  return 1 - e;
+}
+
+_Bool
+bar (_Bool a, _Bool b)
+{
+  int c = 1 - a;
+  int d = 1 - b;
+  _Bool e = c & d;
+  return 1 - e;
+}
+
+_Bool
+baz (_Bool a, _Bool b)
+{
+  _Bool c = 1 - a;
+  _Bool d = 1 - b;
+  _Bool e = c & d;
+  return 1 - e;
+}
+
+/* { dg-final { scan-tree-dump-times "1 - " 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~a" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~b" 0 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." 3 "optimized" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
new file mode 100644
index 00000000000..60d1dbc281e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr108354-1.c
@@ -0,0 +1,26 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int b;
+int *c;
+int e;
+static int *f = &e;
+int g;
+void foo();
+short(a)(short h, short i) { return h - i; }
+int(d)(int h) { return h == 83647 ? 0 : -h; }
+int main() {
+  short j;
+  int *k = &e, *l = &b;
+  *f = 0 == c;
+  j = a(0 != 2, *k);
+  if (d(j ^ (0 == l || *k)) != *k)
+    ;
+  else
+    foo();
+  c = &g;
+}
+
+/* { dg-final { scan-tree-dump-times " 1 - " 0 "optimized" } } */
+/* There should be no calls to foo. */
+/* { dg-final { scan-tree-dump-times "foo " 0 "optimized" } } */
+
-- 
2.17.1


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

end of thread, other threads:[~2023-02-13  6:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-01  1:29 [PATCH] Simplify "1 - bool_val" to "bool_val ^ 1" Andrew Pinski
2023-02-01  7:58 ` Richard Biener
  -- strict thread matches above, loose matches on Subject: below --
2023-02-01  1:21 Andrew Pinski
2023-02-13  6:41 ` 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).