public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/3] Improve ssa_name_has_boolean_range slightly
@ 2023-09-02 15:09 Andrew Pinski
  2023-09-02 15:09 ` [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range Andrew Pinski
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Andrew Pinski @ 2023-09-02 15:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

Right now ssa_name_has_boolean_range compares the range to
range_true_and_false but instead we would get the nonzero bits and
compare that to 1 instead (<=u 1).
The nonzerobits comparison can be done in similar fashion.
Note I think get_nonzero_bits is redundant as the range queury will
return a more accurate version or the same value.

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

gcc/ChangeLog:

	* tree-ssanames.cc (ssa_name_has_boolean_range): Improve
	using range's get_nonzero_bits and use `<=u 1`.
---
 gcc/tree-ssanames.cc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/tree-ssanames.cc b/gcc/tree-ssanames.cc
index 6c362995c1a..7940d9954d8 100644
--- a/gcc/tree-ssanames.cc
+++ b/gcc/tree-ssanames.cc
@@ -535,10 +535,11 @@ ssa_name_has_boolean_range (tree op)
     {
       int_range<2> r;
       if (get_range_query (cfun)->range_of_expr (r, op)
-	  && r == range_true_and_false (TREE_TYPE (op)))
+	  && !r.undefined_p ()
+	  && wi::leu_p (r.get_nonzero_bits (), 1))
 	return true;
 
-      if (wi::eq_p (get_nonzero_bits (op), 1))
+      if (wi::leu_p (get_nonzero_bits (op), 1))
 	return true;
     }
 
-- 
2.31.1


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

* [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range
  2023-09-02 15:09 [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Andrew Pinski
@ 2023-09-02 15:09 ` Andrew Pinski
  2023-09-05  6:59   ` Jeff Law
  2023-09-02 15:09 ` [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p Andrew Pinski
  2023-09-05  6:58 ` [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Jeff Law
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Pinski @ 2023-09-02 15:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

Currently zero_one_valued_p uses tree_nonzero_bits which uses
the global ranges of the SSA Names. We can improve this via using
ssa_name_has_boolean_range which uses the local ranges
which are used while handling folding during VRP and other passes.

OK? Bootstrapped and tested on x86_64 with no regressions.

gcc/ChangeLog:

	* match.pd (zero_one_valued_p): Match SSA_NAMES where
	ssa_name_has_boolean_range returns true.
---
 gcc/match.pd | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index b94d71d2376..04033546fc1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2063,6 +2063,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* zero_one_valued_p will match when a value is known to be either
    0 or 1 including constants 0 or 1.
    Signed 1-bits includes -1 so they cannot match here. */
+/* Note ssa_name_has_boolean_range uses
+   the current ranger while tree_nonzero_bits uses only
+   the global one. */
+(match zero_one_valued_p
+ SSA_NAME@0
+ (if (ssa_name_has_boolean_range (@0))))
 (match zero_one_valued_p
  @0
  (if (INTEGRAL_TYPE_P (type)
-- 
2.31.1


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

* [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p
  2023-09-02 15:09 [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Andrew Pinski
  2023-09-02 15:09 ` [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range Andrew Pinski
@ 2023-09-02 15:09 ` Andrew Pinski
  2023-09-05  7:02   ` Jeff Law
  2023-09-05  6:58 ` [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Jeff Law
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Pinski @ 2023-09-02 15:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

This replaces all uses of ssa_name_has_boolean_range with zero_one_valued_p
except for the one in the definition of zero_one_valued_p. This simplifies
the code in general and makes only one way of saying we have a range of [0,1].

Note this depends on the patch that adds ssa_name_has_boolean_range usage
to zero_one_valued_p.

OK? Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

	* match.pd: Move zero_one_valued_p and truth_valued_p
	towards the begnining of the file.
	(X / bool_range_Y): Use zero_one_valued_p instead
	of ssa_name_has_boolean_range. Move after all other
	`X / Y` patterns. Add check to make sure bool_range_Y
	is not the literal 0.
	(1 - a): Use zero_one_valued_p instead
	of ssa_name_has_boolean_range
	(-(type)!A): Likewise.
---
 gcc/match.pd | 96 ++++++++++++++++++++++++----------------------------
 1 file changed, 45 insertions(+), 51 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 04033546fc1..5270e4104ac 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -172,6 +172,38 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 )
 #endif
 
+/* 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)))
+(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
+ (match truth_valued_p
+  (op @0 @1)))
+(match truth_valued_p
+  (truth_not @0))
+
+/* zero_one_valued_p will match when a value is known to be either
+   0 or 1 including constants 0 or 1.
+   Signed 1-bits includes -1 so they cannot match here. */
+/* Note ssa_name_has_boolean_range uses
+   the current ranger while tree_nonzero_bits uses only
+   the global one. */
+(match zero_one_valued_p
+ SSA_NAME@0
+ (if (ssa_name_has_boolean_range (@0))))
+(match zero_one_valued_p
+ @0
+ (if (INTEGRAL_TYPE_P (type)
+      && (TYPE_UNSIGNED (type)
+	  || TYPE_PRECISION (type) > 1)
+      && wi::leu_p (tree_nonzero_bits (@0), 1))))
+(match zero_one_valued_p
+ truth_valued_p@0
+ (if (INTEGRAL_TYPE_P (type)
+      && (TYPE_UNSIGNED (type)
+	  || TYPE_PRECISION (type) > 1))))
+
 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
    ABSU_EXPR returns unsigned absolute value of the operand and the operand
    of the ABSU_EXPR will have the corresponding signed type.  */
@@ -493,13 +525,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (div @0 integer_minus_onep@1)
   (if (!TYPE_UNSIGNED (type))
    (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)
-       && !flag_non_call_exceptions)
-   @0))
  /* X / X is one.  */
  (simplify
   (div @0 @0)
@@ -525,7 +550,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	&& TYPE_OVERFLOW_UNDEFINED (type)
 	&& !integer_zerop (@0)
 	&& (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
-    { build_minus_one_cst (type); })))
+    { build_minus_one_cst (type); }))
+ /* X / bool_range_Y is X.  */
+ (simplify
+  (div @0 zero_one_valued_p@1)
+  (if (INTEGRAL_TYPE_P (type)
+       /* But not for X / 0 so that we can get the proper warnings and errors. */
+       && !integer_zerop (@1)
+       && !flag_non_call_exceptions)
+   @0)))
 
 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
@@ -1865,14 +1898,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (plus @0 (negate @1))))
 
 /* 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))
+ (minus integer_onep@0 zero_one_valued_p@1)
+  (if (INTEGRAL_TYPE_P (type))
    (bit_xor @1 @0)))
 
 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
@@ -2018,17 +2046,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       (if (cst2)
        (bitop @0 { cst2; }))))))))
 
-/* 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)))
-(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
- (match truth_valued_p
-  (op @0 @1)))
-(match truth_valued_p
-  (truth_not @0))
-
 (match (logical_inverted_value @0)
  (truth_not @0))
 (match (logical_inverted_value @0)
@@ -2060,27 +2077,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (bit_not (bit_not @0))
   @0)
 
-/* zero_one_valued_p will match when a value is known to be either
-   0 or 1 including constants 0 or 1.
-   Signed 1-bits includes -1 so they cannot match here. */
-/* Note ssa_name_has_boolean_range uses
-   the current ranger while tree_nonzero_bits uses only
-   the global one. */
-(match zero_one_valued_p
- SSA_NAME@0
- (if (ssa_name_has_boolean_range (@0))))
-(match zero_one_valued_p
- @0
- (if (INTEGRAL_TYPE_P (type)
-      && (TYPE_UNSIGNED (type)
-	  || TYPE_PRECISION (type) > 1)
-      && wi::leu_p (tree_nonzero_bits (@0), 1))))
-(match zero_one_valued_p
- truth_valued_p@0
- (if (INTEGRAL_TYPE_P (type)
-      && (TYPE_UNSIGNED (type)
-	  || TYPE_PRECISION (type) > 1))))
-
 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
 (simplify
  (mult zero_one_valued_p@0 zero_one_valued_p@1)
@@ -5486,12 +5482,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 zero_one_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.31.1


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

* Re: [PATCH 1/3] Improve ssa_name_has_boolean_range slightly
  2023-09-02 15:09 [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Andrew Pinski
  2023-09-02 15:09 ` [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range Andrew Pinski
  2023-09-02 15:09 ` [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p Andrew Pinski
@ 2023-09-05  6:58 ` Jeff Law
  2 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2023-09-05  6:58 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



On 9/2/23 09:09, Andrew Pinski via Gcc-patches wrote:
> Right now ssa_name_has_boolean_range compares the range to
> range_true_and_false but instead we would get the nonzero bits and
> compare that to 1 instead (<=u 1).
> The nonzerobits comparison can be done in similar fashion.
> Note I think get_nonzero_bits is redundant as the range queury will
> return a more accurate version or the same value.
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> 
> gcc/ChangeLog:
> 
> 	* tree-ssanames.cc (ssa_name_has_boolean_range): Improve
> 	using range's get_nonzero_bits and use `<=u 1`.
OK
jeff

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

* Re: [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range
  2023-09-02 15:09 ` [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range Andrew Pinski
@ 2023-09-05  6:59   ` Jeff Law
  0 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2023-09-05  6:59 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



On 9/2/23 09:09, Andrew Pinski via Gcc-patches wrote:
> Currently zero_one_valued_p uses tree_nonzero_bits which uses
> the global ranges of the SSA Names. We can improve this via using
> ssa_name_has_boolean_range which uses the local ranges
> which are used while handling folding during VRP and other passes.
> 
> OK? Bootstrapped and tested on x86_64 with no regressions.
> 
> gcc/ChangeLog:
> 
> 	* match.pd (zero_one_valued_p): Match SSA_NAMES where
> 	ssa_name_has_boolean_range returns true.
OK.  Presumably we're confident using a context sensitive range is 
always OK in here.

Jeff

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

* Re: [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p
  2023-09-02 15:09 ` [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p Andrew Pinski
@ 2023-09-05  7:02   ` Jeff Law
  0 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2023-09-05  7:02 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



On 9/2/23 09:09, Andrew Pinski via Gcc-patches wrote:
> This replaces all uses of ssa_name_has_boolean_range with zero_one_valued_p
> except for the one in the definition of zero_one_valued_p. This simplifies
> the code in general and makes only one way of saying we have a range of [0,1].
> 
> Note this depends on the patch that adds ssa_name_has_boolean_range usage
> to zero_one_valued_p.
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu.
> 
> gcc/ChangeLog:
> 
> 	* match.pd: Move zero_one_valued_p and truth_valued_p
> 	towards the begnining of the file.
> 	(X / bool_range_Y): Use zero_one_valued_p instead
> 	of ssa_name_has_boolean_range. Move after all other
> 	`X / Y` patterns. Add check to make sure bool_range_Y
> 	is not the literal 0.
> 	(1 - a): Use zero_one_valued_p instead
> 	of ssa_name_has_boolean_range
> 	(-(type)!A): Likewise.
OK.
jeff

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

end of thread, other threads:[~2023-09-05  7:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-02 15:09 [PATCH 1/3] Improve ssa_name_has_boolean_range slightly Andrew Pinski
2023-09-02 15:09 ` [PATCH 2/3] MATCH: Improve zero_one_valued_p by using ssa_name_has_boolean_range Andrew Pinski
2023-09-05  6:59   ` Jeff Law
2023-09-02 15:09 ` [PATCH 3/3] MATCH: Replace all uses of ssa_name_has_boolean_range with zero_one_valued_p Andrew Pinski
2023-09-05  7:02   ` Jeff Law
2023-09-05  6:58 ` [PATCH 1/3] Improve ssa_name_has_boolean_range slightly 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).