public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCHv2] Fix PR 110954: wrong code with cmp | !cmp
@ 2023-08-10 22:51 Andrew Pinski
  2023-08-11  6:46 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-08-10 22:51 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

This was an oversight on my part forgetting that
cmp will might have a different true value than all ones
but will have a value of 1 in most cases.
This means if we have `(f < 0) | !(f < 0)` we would
optimize this to -1 rather than just 1.

This is version 2 of the patch.
Decided to go down a different route than just checking if
the precission was 1 inside bitwise_inverted_equal_p.
So instead bitwise_inverted_equal_p gets passed an argument
that will be set if there was a comparison that was being compared
and the user of bitwise_inverted_equal_p decides what needs to be done.
In most uses of bitwise_inverted_equal_p, the check will be
`!wascmp || element_precision (type) == 1` .
But in the case of `a & ~a` and `a ^| ~a` we can handle the case
of wascmp by using constant_boolean_node isntead.

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

	PR 110954

gcc/ChangeLog:

	* generic-match-head.cc (bitwise_inverted_equal_p): Add
	wascmp argument and set it accordingly.
	* gimple-match-head.cc (bitwise_inverted_equal_p): Add
	wascmp argument to the macro.
	(gimple_bitwise_inverted_equal_p): Add
	wascmp argument and set it accordingly.
	* match.pd (`a & ~a`, `a ^| ~a`): Update call
	to bitwise_inverted_equal_p and handle wascmp case.
	(`(~x | y) & x`, `(~x | y) & x`, `a?~t:t`): Update
	call to bitwise_inverted_equal_p and check to see
	if was !wascmp or if precision was 1.

gcc/testsuite/ChangeLog:

	* gcc.c-torture/execute/pr110954-1.c: New test.
---
 gcc/generic-match-head.cc                     |  4 +-
 gcc/gimple-match-head.cc                      |  8 ++--
 gcc/match.pd                                  | 41 +++++++++++--------
 .../gcc.c-torture/execute/pr110954-1.c        | 10 +++++
 4 files changed, 43 insertions(+), 20 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110954-1.c

diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc
index ddaf22f2179..f40a35c48a6 100644
--- a/gcc/generic-match-head.cc
+++ b/gcc/generic-match-head.cc
@@ -127,10 +127,11 @@ bitwise_equal_p (tree expr1, tree expr2)
    The types can differ through nop conversions.  */
 
 static inline bool
-bitwise_inverted_equal_p (tree expr1, tree expr2)
+bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp)
 {
   STRIP_NOPS (expr1);
   STRIP_NOPS (expr2);
+  wascmp = false;
   if (expr1 == expr2)
     return false;
   if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
@@ -150,6 +151,7 @@ bitwise_inverted_equal_p (tree expr1, tree expr2)
     {
       tree op10 = TREE_OPERAND (expr1, 0);
       tree op20 = TREE_OPERAND (expr2, 0);
+      wascmp = true;
       if (!operand_equal_p (op10, op20))
 	return false;
       tree op11 = TREE_OPERAND (expr1, 1);
diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index a097a494c39..ea6387a1099 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -267,8 +267,8 @@ gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree))
 /* Return true if EXPR1 and EXPR2 have the bitwise opposite value,
    but not necessarily same type.
    The types can differ through nop conversions.  */
-#define bitwise_inverted_equal_p(expr1, expr2) \
-  gimple_bitwise_inverted_equal_p (expr1, expr2, valueize)
+#define bitwise_inverted_equal_p(expr1, expr2, wascmp) \
+  gimple_bitwise_inverted_equal_p (expr1, expr2, wascmp, valueize)
 
 
 bool gimple_bit_not_with_nop (tree, tree *, tree (*) (tree));
@@ -277,8 +277,9 @@ bool gimple_maybe_cmp (tree, tree *, tree (*) (tree));
 /* Helper function for bitwise_equal_p macro.  */
 
 static inline bool
-gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree))
+gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp, tree (*valueize) (tree))
 {
+  wascmp = false;
   if (expr1 == expr2)
     return false;
   if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
@@ -331,6 +332,7 @@ gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)
   tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2));
   if (!operand_equal_p (op11, op21))
     return false;
+  wascmp = true;
   if (invert_tree_comparison (gimple_assign_rhs_code (a1),
 			      HONOR_NANS (op10))
 	== gimple_assign_rhs_code (a2))
diff --git a/gcc/match.pd b/gcc/match.pd
index fc630b63563..6791060891d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1179,9 +1179,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* Simplify ~X & X as zero.  */
 (simplify
  (bit_and (convert? @0) (convert? @1))
- (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
-      && bitwise_inverted_equal_p (@0, @1))
-  { build_zero_cst (type); }))
+ (with { bool wascmp; }
+  (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+       && bitwise_inverted_equal_p (@0, @1, wascmp))
+   { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
 
 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
 (simplify
@@ -1419,9 +1420,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for op (bit_ior bit_xor)
  (simplify
   (op (convert? @0) (convert? @1))
-  (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
-       && bitwise_inverted_equal_p (@0, @1))
-   (convert { build_all_ones_cst (TREE_TYPE (@0)); }))))
+  (with { bool wascmp; }
+   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+        && bitwise_inverted_equal_p (@0, @1, wascmp))
+    (convert
+     { wascmp
+        ? constant_boolean_node (true, type)
+	: build_all_ones_cst (TREE_TYPE (@0)); })))))
 
 /* x ^ x -> 0 */
 (simplify
@@ -1969,8 +1974,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  /* (~x & y) | x -> x | y */
  (simplify
   (bitop:c (rbitop:c @2 @1) @0)
-  (if (bitwise_inverted_equal_p (@0, @2))
-   (bitop @0 @1))))
+  (with { bool wascmp; }
+   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
+	&& (!wascmp || element_precision (type) == 1))
+    (bitop @0 @1)))))
 
 /* ((x | y) & z) | x -> (z & y) | x */
 (simplify
@@ -6464,14 +6471,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* a?~t:t -> (-(a))^t */
 (simplify
  (cond @0 @1 @2)
- (if (INTEGRAL_TYPE_P (type)
-      && bitwise_inverted_equal_p (@1, @2))
-  (with {
-    auto prec = TYPE_PRECISION (type);
-    auto unsign = TYPE_UNSIGNED (type);
-    tree inttype = build_nonstandard_integer_type (prec, unsign);
-   }
-   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))))))
+ (with { bool wascmp; }
+  (if (INTEGRAL_TYPE_P (type)
+       && bitwise_inverted_equal_p (@1, @2, wascmp)
+       && (!wascmp || element_precision (type) == 1))
+   (with {
+     auto prec = TYPE_PRECISION (type);
+     auto unsign = TYPE_UNSIGNED (type);
+     tree inttype = build_nonstandard_integer_type (prec, unsign);
+    }
+    (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
 #endif
 
 /* Simplify pointer equality compares using PTA.  */
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c
new file mode 100644
index 00000000000..8aad758e10f
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c
@@ -0,0 +1,10 @@
+
+#define comparison (f < 0)
+int main() {
+  int f = 0;
+  int d = comparison | !comparison;
+  if (d != 1)
+    __builtin_abort();
+  return 0;
+}
+
-- 
2.31.1


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

* Re: [PATCHv2] Fix PR 110954: wrong code with cmp | !cmp
  2023-08-10 22:51 [PATCHv2] Fix PR 110954: wrong code with cmp | !cmp Andrew Pinski
@ 2023-08-11  6:46 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-08-11  6:46 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Fri, Aug 11, 2023 at 12:52 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This was an oversight on my part forgetting that
> cmp will might have a different true value than all ones
> but will have a value of 1 in most cases.
> This means if we have `(f < 0) | !(f < 0)` we would
> optimize this to -1 rather than just 1.
>
> This is version 2 of the patch.
> Decided to go down a different route than just checking if
> the precission was 1 inside bitwise_inverted_equal_p.
> So instead bitwise_inverted_equal_p gets passed an argument
> that will be set if there was a comparison that was being compared
> and the user of bitwise_inverted_equal_p decides what needs to be done.
> In most uses of bitwise_inverted_equal_p, the check will be
> `!wascmp || element_precision (type) == 1` .
> But in the case of `a & ~a` and `a ^| ~a` we can handle the case
> of wascmp by using constant_boolean_node isntead.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

Thanks,
Richard.

>         PR 110954
>
> gcc/ChangeLog:
>
>         * generic-match-head.cc (bitwise_inverted_equal_p): Add
>         wascmp argument and set it accordingly.
>         * gimple-match-head.cc (bitwise_inverted_equal_p): Add
>         wascmp argument to the macro.
>         (gimple_bitwise_inverted_equal_p): Add
>         wascmp argument and set it accordingly.
>         * match.pd (`a & ~a`, `a ^| ~a`): Update call
>         to bitwise_inverted_equal_p and handle wascmp case.
>         (`(~x | y) & x`, `(~x | y) & x`, `a?~t:t`): Update
>         call to bitwise_inverted_equal_p and check to see
>         if was !wascmp or if precision was 1.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/execute/pr110954-1.c: New test.
> ---
>  gcc/generic-match-head.cc                     |  4 +-
>  gcc/gimple-match-head.cc                      |  8 ++--
>  gcc/match.pd                                  | 41 +++++++++++--------
>  .../gcc.c-torture/execute/pr110954-1.c        | 10 +++++
>  4 files changed, 43 insertions(+), 20 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110954-1.c
>
> diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc
> index ddaf22f2179..f40a35c48a6 100644
> --- a/gcc/generic-match-head.cc
> +++ b/gcc/generic-match-head.cc
> @@ -127,10 +127,11 @@ bitwise_equal_p (tree expr1, tree expr2)
>     The types can differ through nop conversions.  */
>
>  static inline bool
> -bitwise_inverted_equal_p (tree expr1, tree expr2)
> +bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp)
>  {
>    STRIP_NOPS (expr1);
>    STRIP_NOPS (expr2);
> +  wascmp = false;
>    if (expr1 == expr2)
>      return false;
>    if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
> @@ -150,6 +151,7 @@ bitwise_inverted_equal_p (tree expr1, tree expr2)
>      {
>        tree op10 = TREE_OPERAND (expr1, 0);
>        tree op20 = TREE_OPERAND (expr2, 0);
> +      wascmp = true;
>        if (!operand_equal_p (op10, op20))
>         return false;
>        tree op11 = TREE_OPERAND (expr1, 1);
> diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
> index a097a494c39..ea6387a1099 100644
> --- a/gcc/gimple-match-head.cc
> +++ b/gcc/gimple-match-head.cc
> @@ -267,8 +267,8 @@ gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree))
>  /* Return true if EXPR1 and EXPR2 have the bitwise opposite value,
>     but not necessarily same type.
>     The types can differ through nop conversions.  */
> -#define bitwise_inverted_equal_p(expr1, expr2) \
> -  gimple_bitwise_inverted_equal_p (expr1, expr2, valueize)
> +#define bitwise_inverted_equal_p(expr1, expr2, wascmp) \
> +  gimple_bitwise_inverted_equal_p (expr1, expr2, wascmp, valueize)
>
>
>  bool gimple_bit_not_with_nop (tree, tree *, tree (*) (tree));
> @@ -277,8 +277,9 @@ bool gimple_maybe_cmp (tree, tree *, tree (*) (tree));
>  /* Helper function for bitwise_equal_p macro.  */
>
>  static inline bool
> -gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree))
> +gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp, tree (*valueize) (tree))
>  {
> +  wascmp = false;
>    if (expr1 == expr2)
>      return false;
>    if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2)))
> @@ -331,6 +332,7 @@ gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)
>    tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2));
>    if (!operand_equal_p (op11, op21))
>      return false;
> +  wascmp = true;
>    if (invert_tree_comparison (gimple_assign_rhs_code (a1),
>                               HONOR_NANS (op10))
>         == gimple_assign_rhs_code (a2))
> diff --git a/gcc/match.pd b/gcc/match.pd
> index fc630b63563..6791060891d 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -1179,9 +1179,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  /* Simplify ~X & X as zero.  */
>  (simplify
>   (bit_and (convert? @0) (convert? @1))
> - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
> -      && bitwise_inverted_equal_p (@0, @1))
> -  { build_zero_cst (type); }))
> + (with { bool wascmp; }
> +  (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
> +       && bitwise_inverted_equal_p (@0, @1, wascmp))
> +   { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
>
>  /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
>  (simplify
> @@ -1419,9 +1420,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  (for op (bit_ior bit_xor)
>   (simplify
>    (op (convert? @0) (convert? @1))
> -  (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
> -       && bitwise_inverted_equal_p (@0, @1))
> -   (convert { build_all_ones_cst (TREE_TYPE (@0)); }))))
> +  (with { bool wascmp; }
> +   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
> +        && bitwise_inverted_equal_p (@0, @1, wascmp))
> +    (convert
> +     { wascmp
> +        ? constant_boolean_node (true, type)
> +       : build_all_ones_cst (TREE_TYPE (@0)); })))))
>
>  /* x ^ x -> 0 */
>  (simplify
> @@ -1969,8 +1974,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   /* (~x & y) | x -> x | y */
>   (simplify
>    (bitop:c (rbitop:c @2 @1) @0)
> -  (if (bitwise_inverted_equal_p (@0, @2))
> -   (bitop @0 @1))))
> +  (with { bool wascmp; }
> +   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
> +       && (!wascmp || element_precision (type) == 1))
> +    (bitop @0 @1)))))
>
>  /* ((x | y) & z) | x -> (z & y) | x */
>  (simplify
> @@ -6464,14 +6471,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>  /* a?~t:t -> (-(a))^t */
>  (simplify
>   (cond @0 @1 @2)
> - (if (INTEGRAL_TYPE_P (type)
> -      && bitwise_inverted_equal_p (@1, @2))
> -  (with {
> -    auto prec = TYPE_PRECISION (type);
> -    auto unsign = TYPE_UNSIGNED (type);
> -    tree inttype = build_nonstandard_integer_type (prec, unsign);
> -   }
> -   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))))))
> + (with { bool wascmp; }
> +  (if (INTEGRAL_TYPE_P (type)
> +       && bitwise_inverted_equal_p (@1, @2, wascmp)
> +       && (!wascmp || element_precision (type) == 1))
> +   (with {
> +     auto prec = TYPE_PRECISION (type);
> +     auto unsign = TYPE_UNSIGNED (type);
> +     tree inttype = build_nonstandard_integer_type (prec, unsign);
> +    }
> +    (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
>  #endif
>
>  /* Simplify pointer equality compares using PTA.  */
> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c
> new file mode 100644
> index 00000000000..8aad758e10f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c
> @@ -0,0 +1,10 @@
> +
> +#define comparison (f < 0)
> +int main() {
> +  int f = 0;
> +  int d = comparison | !comparison;
> +  if (d != 1)
> +    __builtin_abort();
> +  return 0;
> +}
> +
> --
> 2.31.1
>

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

end of thread, other threads:[~2023-08-11  6:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-10 22:51 [PATCHv2] Fix PR 110954: wrong code with cmp | !cmp Andrew Pinski
2023-08-11  6:46 ` 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).