public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it
@ 2021-08-14  0:19 apinski
  2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: apinski @ 2021-08-14  0:19 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

While working on some more boolean optimizations, I noticed
that there are places which does SSA_NAME@0 and then look
at then either use get_nonzero_bits or ssa_name_has_boolean_range
to see if the ssa name had a boolean range. This cleans this
up slightly by have a simple match pattern call gimple_truth_valued_p
which matches on SSA_NAME and checks ssa_name_has_boolean_range.
This is the first of the few cleanups I am going to do for
match and simplify and boolean related changes.

gcc/ChangeLog:

	* match.pd: New match, gimple_truth_valued_p.
	Use it for "{ 0 or 1 } * { 0 or 1 }",
	"X / bool_range_Y", and "-(type)!A" simplifcations.
---
 gcc/match.pd | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5cc6a9fd41c..b1f2aaaac02 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -98,6 +98,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (define_operator_list COND_TERNARY
   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
 
+/* Match for a SSA_NAME which has a range of [0,1] */
+(match gimple_truth_valued_p
+ SSA_NAME@0
+ (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@0))))
+
 /* With nop_convert? combine convert? and view_convert? in one pattern
    plus conditionalize on tree_nop_conversion_p conversions.  */
 (match (nop_convert @0)
@@ -230,11 +235,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
 (simplify
- (mult SSA_NAME@1 SSA_NAME@2)
-  (if (INTEGRAL_TYPE_P (type)
-       && get_nonzero_bits (@1) == 1
-       && get_nonzero_bits (@2) == 1)
-   (bit_and @1 @2)))
+ (mult gimple_truth_valued_p@1 gimple_truth_valued_p@2)
+  (bit_and @1 @2))
 
 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
    unless the target has native support for the former but not the latter.  */
@@ -347,9 +349,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (negate @0)))
  /* X / bool_range_Y is X.  */ 
  (simplify
-  (div @0 SSA_NAME@1)
-  (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
-   @0))
+  (div @0 gimple_truth_valued_p@1)
+   @0)
  /* X / X is one.  */
  (simplify
   (div @0 @0)
@@ -4207,12 +4208,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* -(type)!A -> (type)A - 1.  */
 (simplify
- (negate (convert?:s (logical_inverted_value:s @0)))
+ (negate (convert?:s (logical_inverted_value:s gimple_truth_valued_p@0)))
  (if (INTEGRAL_TYPE_P (type)
       && TREE_CODE (type) != BOOLEAN_TYPE
-      && TYPE_PRECISION (type) > 1
-      && TREE_CODE (@0) == SSA_NAME
-      && ssa_name_has_boolean_range (@0))
+      && TYPE_PRECISION (type) > 1)
   (plus (convert:type @0) { build_all_ones_cst (type); })))
 
 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
-- 
2.27.0


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

* [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments
  2021-08-14  0:19 [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it apinski
@ 2021-08-14  0:19 ` apinski
  2021-08-16  6:29   ` Richard Biener
  2021-09-19  5:56   ` Jeff Law
  2021-08-16  6:22 ` [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it Richard Biener
  2021-09-19  5:55 ` Jeff Law
  2 siblings, 2 replies; 6+ messages in thread
From: apinski @ 2021-08-14  0:19 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

I noticed this while Richard B. fixing PR101756.
Basically min of two bools is the same as doing an "and"
and max of two bools is doing an "ior".

gcc/ChangeLog:

	* match.pd: Add min/max patterns for bool types.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/bool-12.c: New test.
---
 gcc/match.pd                            | 10 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c | 27 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c

diff --git a/gcc/match.pd b/gcc/match.pd
index b1f2aaaac02..8fd60d08cfe 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3103,6 +3103,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
    (cond (cmp @2 @3) @1 @0))))
 
+/* max(bool0, bool1) -> bool0 | bool1 */
+(simplify
+ (max gimple_truth_valued_p@0 gimple_truth_valued_p@1)
+ (bit_ior @0 @1))
+
+/* min(bool0, bool1) -> bool0 & bool1 */
+(simplify
+ (min gimple_truth_valued_p@0 gimple_truth_valued_p@1)
+ (bit_and @0 @1))
+
 /* Simplifications of shift and rotates.  */
 
 (for rotate (lrotate rrotate)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
new file mode 100644
index 00000000000..2d8ad9912d3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original" } */
+#define bool _Bool
+int maxbool(bool ab, bool bb)
+{
+  int a = ab;
+  int b = bb;
+  int c;
+  c = (a > b)?a : b;
+  return c;
+}
+int minbool(bool ab, bool bb)
+{
+  int a = ab;
+  int b = bb;
+  int c;
+  c = (a < b)?a : b;
+  return c;
+}
+/* Original should have one of each MAX/MIN expressions. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "original" } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original"} } */
+
+/* By the time we reach optimized, the MAX and MIN expressions
+   should have been removed. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized"} } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized"} } */
-- 
2.27.0


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

* Re: [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it
  2021-08-14  0:19 [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it apinski
  2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
@ 2021-08-16  6:22 ` Richard Biener
  2021-09-19  5:55 ` Jeff Law
  2 siblings, 0 replies; 6+ messages in thread
From: Richard Biener @ 2021-08-16  6:22 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

On Sat, Aug 14, 2021 at 2:20 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> While working on some more boolean optimizations, I noticed
> that there are places which does SSA_NAME@0 and then look
> at then either use get_nonzero_bits or ssa_name_has_boolean_range
> to see if the ssa name had a boolean range. This cleans this
> up slightly by have a simple match pattern call gimple_truth_valued_p
> which matches on SSA_NAME and checks ssa_name_has_boolean_range.
> This is the first of the few cleanups I am going to do for
> match and simplify and boolean related changes.
>
> gcc/ChangeLog:
>
>         * match.pd: New match, gimple_truth_valued_p.
>         Use it for "{ 0 or 1 } * { 0 or 1 }",
>         "X / bool_range_Y", and "-(type)!A" simplifcations.
> ---
>  gcc/match.pd | 23 +++++++++++------------
>  1 file changed, 11 insertions(+), 12 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 5cc6a9fd41c..b1f2aaaac02 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -98,6 +98,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (define_operator_list COND_TERNARY
>    IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
>
> +/* Match for a SSA_NAME which has a range of [0,1] */
> +(match gimple_truth_valued_p
> + SSA_NAME@0
> + (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@0))))
> +

So we already have

/* Try simple folding for X op !X, and X op X with the help
   of the truth_valued_p and logical_inverted_value predicates.  */
(match truth_valued_p
 @0
 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))

IMHO gimple_truth_valued_p is easily to be confused with this.  I wonder whether
you can simply add

#if GIMPLE
(match truth_valued_p
  SSA_NAME@0
  (if (...
#endif

and use truth_valued_p?

>  /* With nop_convert? combine convert? and view_convert? in one pattern
>     plus conditionalize on tree_nop_conversion_p conversions.  */
>  (match (nop_convert @0)
> @@ -230,11 +235,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
>  (simplify
> - (mult SSA_NAME@1 SSA_NAME@2)
> -  (if (INTEGRAL_TYPE_P (type)
> -       && get_nonzero_bits (@1) == 1
> -       && get_nonzero_bits (@2) == 1)
> -   (bit_and @1 @2)))
> + (mult gimple_truth_valued_p@1 gimple_truth_valued_p@2)
> +  (bit_and @1 @2))
>
>  /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
>     unless the target has native support for the former but not the latter.  */
> @@ -347,9 +349,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>     (negate @0)))
>   /* X / bool_range_Y is X.  */
>   (simplify
> -  (div @0 SSA_NAME@1)
> -  (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
> -   @0))
> +  (div @0 gimple_truth_valued_p@1)
> +   @0)
>   /* X / X is one.  */
>   (simplify
>    (div @0 @0)
> @@ -4207,12 +4208,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* -(type)!A -> (type)A - 1.  */
>  (simplify
> - (negate (convert?:s (logical_inverted_value:s @0)))
> + (negate (convert?:s (logical_inverted_value:s gimple_truth_valued_p@0)))
>   (if (INTEGRAL_TYPE_P (type)
>        && TREE_CODE (type) != BOOLEAN_TYPE
> -      && TYPE_PRECISION (type) > 1
> -      && TREE_CODE (@0) == SSA_NAME
> -      && ssa_name_has_boolean_range (@0))
> +      && TYPE_PRECISION (type) > 1)
>    (plus (convert:type @0) { build_all_ones_cst (type); })))
>
>  /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
> --
> 2.27.0
>

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

* Re: [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments
  2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
@ 2021-08-16  6:29   ` Richard Biener
  2021-09-19  5:56   ` Jeff Law
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Biener @ 2021-08-16  6:29 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches

On Sat, Aug 14, 2021 at 2:21 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> From: Andrew Pinski <apinski@marvell.com>
>
> I noticed this while Richard B. fixing PR101756.
> Basically min of two bools is the same as doing an "and"
> and max of two bools is doing an "ior".

But that's only true for unsigned vals.  For signed ones it would
be the other way around and also restricted to 1-bit precision bools.
For signed bools the gimple_truth_valued_p check likely is wrong as well
unless 1-bit precision?  IIRC Ada has non-1 bit precision BOOLEAN_TYPE
nodes while fortran bools have 1-bit precision but different sizes.  The
vector bools are IIRC the only 'signed' bools we have and those have
precisions != 1.  I think we can use the fact that any non -1/0/1 value
in a N-bit precision bool invokes undefined behavior though.  But whether
in a N-bit signed precision bool the canonical true value is -1 or 1 isn't
as clear (maybe we can resort to TYPE_MIN/MAX_VALUE here, not sure).

Way out would be to restrict all this to TYPE_UNSIGNED BOOLEAN_TYPE
or non-BOOLEAN_TYPE?

Richard.

> gcc/ChangeLog:
>
>         * match.pd: Add min/max patterns for bool types.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/bool-12.c: New test.
> ---
>  gcc/match.pd                            | 10 +++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-12.c | 27 +++++++++++++++++++++++++
>  2 files changed, 37 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index b1f2aaaac02..8fd60d08cfe 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3103,6 +3103,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>         && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
>     (cond (cmp @2 @3) @1 @0))))
>
> +/* max(bool0, bool1) -> bool0 | bool1 */
> +(simplify
> + (max gimple_truth_valued_p@0 gimple_truth_valued_p@1)
> + (bit_ior @0 @1))
> +
> +/* min(bool0, bool1) -> bool0 & bool1 */
> +(simplify
> + (min gimple_truth_valued_p@0 gimple_truth_valued_p@1)
> + (bit_and @0 @1))
> +
>  /* Simplifications of shift and rotates.  */
>
>  (for rotate (lrotate rrotate)
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
> new file mode 100644
> index 00000000000..2d8ad9912d3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c
> @@ -0,0 +1,27 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original" } */
> +#define bool _Bool
> +int maxbool(bool ab, bool bb)
> +{
> +  int a = ab;
> +  int b = bb;
> +  int c;
> +  c = (a > b)?a : b;
> +  return c;
> +}
> +int minbool(bool ab, bool bb)
> +{
> +  int a = ab;
> +  int b = bb;
> +  int c;
> +  c = (a < b)?a : b;
> +  return c;
> +}
> +/* Original should have one of each MAX/MIN expressions. */
> +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "original" } */
> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original"} } */
> +
> +/* By the time we reach optimized, the MAX and MIN expressions
> +   should have been removed. */
> +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized"} } */
> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized"} } */
> --
> 2.27.0
>

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

* Re: [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it
  2021-08-14  0:19 [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it apinski
  2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
  2021-08-16  6:22 ` [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it Richard Biener
@ 2021-09-19  5:55 ` Jeff Law
  2 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2021-09-19  5:55 UTC (permalink / raw)
  To: apinski, gcc-patches



On 8/13/2021 6:19 PM, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
>
> While working on some more boolean optimizations, I noticed
> that there are places which does SSA_NAME@0 and then look
> at then either use get_nonzero_bits or ssa_name_has_boolean_range
> to see if the ssa name had a boolean range. This cleans this
> up slightly by have a simple match pattern call gimple_truth_valued_p
> which matches on SSA_NAME and checks ssa_name_has_boolean_range.
> This is the first of the few cleanups I am going to do for
> match and simplify and boolean related changes.
>
> gcc/ChangeLog:
>
> 	* match.pd: New match, gimple_truth_valued_p.
> 	Use it for "{ 0 or 1 } * { 0 or 1 }",
> 	"X / bool_range_Y", and "-(type)!A" simplifcations.
OK
jeff


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

* Re: [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments
  2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
  2021-08-16  6:29   ` Richard Biener
@ 2021-09-19  5:56   ` Jeff Law
  1 sibling, 0 replies; 6+ messages in thread
From: Jeff Law @ 2021-09-19  5:56 UTC (permalink / raw)
  To: apinski, gcc-patches



On 8/13/2021 6:19 PM, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
>
> I noticed this while Richard B. fixing PR101756.
> Basically min of two bools is the same as doing an "and"
> and max of two bools is doing an "ior".
>
> gcc/ChangeLog:
>
> 	* match.pd: Add min/max patterns for bool types.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.dg/tree-ssa/bool-12.c: New test.
OK
jeff


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

end of thread, other threads:[~2021-09-19  5:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-14  0:19 [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it apinski
2021-08-14  0:19 ` [PATCH 2/2] Fix 101805: Simplify min/max of boolean arguments apinski
2021-08-16  6:29   ` Richard Biener
2021-09-19  5:56   ` Jeff Law
2021-08-16  6:22 ` [PATCH 1/2] Add gimple_truth_valued_p to match.pd and use it Richard Biener
2021-09-19  5:55 ` 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).