public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix PR 110666: `(a != 2) == a` produces wrong code
@ 2023-07-14 20:55 Andrew Pinski
  2023-07-17  6:20 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-07-14 20:55 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

I had messed up the case where the outer operator is `==`.
The check for the resulting should have been `==` and not `!=`.
This patch fixes that and adds a full runtime testcase now for
all cases to make sure it works.

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

gcc/ChangeLog:

	PR tree-optimization/110666
	* match.pd (A NEEQ (A NEEQ CST)): Fix Outer EQ case.

gcc/testsuite/ChangeLog:

	PR tree-optimization/110666
	* gcc.c-torture/execute/pr110666-1.c: New test.
---
 gcc/match.pd                                  | 34 ++++++++-----
 .../gcc.c-torture/execute/pr110666-1.c        | 51 +++++++++++++++++++
 2 files changed, 71 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110666-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 351d9285e92..88061fa4a6f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6431,8 +6431,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
-/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x != 0) */
-/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x != 1) */
+/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
+/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
 (for outer (ne eq)
  (for inner (ne eq)
   (simplify
@@ -6443,23 +6443,29 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
      bool innereq = inner == EQ_EXPR;
      bool outereq = outer == EQ_EXPR;
     }
-   (switch
-    (if (innereq ? cst0 : cst1)
-     { constant_boolean_node (!outereq, type); })
-    (if (innereq ? cst1 : cst0)
+    (switch
+     (if (innereq ? cst0 : cst1)
+      { constant_boolean_node (!outereq, type); })
+     (if (innereq ? cst1 : cst0)
+      (with {
+        tree utype = unsigned_type_for (TREE_TYPE (@0));
+        tree ucst1 = build_one_cst (utype);
+       }
+       (if (!outereq)
+        (gt (convert:utype @0) { ucst1; })
+        (le (convert:utype @0) { ucst1; })
+       )
+      )
+     )
      (with {
-       tree utype = unsigned_type_for (TREE_TYPE (@0));
-       tree ucst1 = build_one_cst (utype);
+       tree value = build_int_cst (TREE_TYPE (@0), !innereq);
       }
-      (if (!outereq)
-       (gt (convert:utype @0) { ucst1; })
-       (le (convert:utype @0) { ucst1; })
+      (if (outereq)
+       (eq @0 { value; })
+       (ne @0 { value; })
       )
      )
     )
-    (if (innereq)
-     (ne @0 { build_zero_cst (TREE_TYPE (@0)); }))
-    (ne @0 { build_one_cst (TREE_TYPE (@0)); }))
    )
   )
  )
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c
new file mode 100644
index 00000000000..b22eb7781da
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c
@@ -0,0 +1,51 @@
+
+#define func_name(outer,inner,cst) outer##inner##_##cst
+#define func_name_v(outer,inner,cst) outer##inner##_##cst##_v
+
+#define func_decl(outer,inner,cst) \
+int outer##inner##_##cst (int) __attribute__((noipa)); \
+int outer##inner##_##cst (int a) { \
+  return (a op_##inner cst) op_##outer a; \
+} \
+int outer##inner##_##cst##_v (int) __attribute__((noipa)); \
+int outer##inner##_##cst##_v (volatile int a) { \
+  return (a op_##inner cst) op_##outer a; \
+}
+
+#define functions_n(outer, inner) \
+func_decl(outer,inner,0) \
+func_decl(outer,inner,1) \
+func_decl(outer,inner,2)
+
+#define functions() \
+functions_n(eq,eq) \
+functions_n(eq,ne) \
+functions_n(ne,eq) \
+functions_n(ne,ne)
+
+#define op_ne !=
+#define op_eq ==
+
+#define test(inner,outer,cst,arg) \
+func_name_v (inner,outer,cst)(arg) != func_name(inner,outer,cst)(arg)
+
+functions()
+
+#define tests_n(inner,outer,arg) \
+if (test(inner,outer,0,arg)) __builtin_abort(); \
+if (test(inner,outer,1,arg)) __builtin_abort(); \
+if (test(inner,outer,2,arg)) __builtin_abort();
+
+#define tests(arg) \
+tests_n(eq,eq,arg) \
+tests_n(eq,ne,arg) \
+tests_n(ne,eq,arg) \
+tests_n(ne,ne,arg)
+
+
+int main()
+{
+  for(int n = -1; n <= 2; n++) {
+    tests(n)
+  }
+}
-- 
2.31.1


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

* Re: [PATCH] Fix PR 110666: `(a != 2) == a` produces wrong code
  2023-07-14 20:55 [PATCH] Fix PR 110666: `(a != 2) == a` produces wrong code Andrew Pinski
@ 2023-07-17  6:20 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-07-17  6:20 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Fri, Jul 14, 2023 at 10:56 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> I had messed up the case where the outer operator is `==`.
> The check for the resulting should have been `==` and not `!=`.
> This patch fixes that and adds a full runtime testcase now for
> all cases to make sure it works.
>
> OK? Bootstrapped and tested on x86-64-linux-gnu with no regressions.

OK.

> gcc/ChangeLog:
>
>         PR tree-optimization/110666
>         * match.pd (A NEEQ (A NEEQ CST)): Fix Outer EQ case.
>
> gcc/testsuite/ChangeLog:
>
>         PR tree-optimization/110666
>         * gcc.c-torture/execute/pr110666-1.c: New test.
> ---
>  gcc/match.pd                                  | 34 ++++++++-----
>  .../gcc.c-torture/execute/pr110666-1.c        | 51 +++++++++++++++++++
>  2 files changed, 71 insertions(+), 14 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110666-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 351d9285e92..88061fa4a6f 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -6431,8 +6431,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
>  /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
>  /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
> -/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x != 0) */
> -/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x != 1) */
> +/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
> +/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
>  (for outer (ne eq)
>   (for inner (ne eq)
>    (simplify
> @@ -6443,23 +6443,29 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>       bool innereq = inner == EQ_EXPR;
>       bool outereq = outer == EQ_EXPR;
>      }
> -   (switch
> -    (if (innereq ? cst0 : cst1)
> -     { constant_boolean_node (!outereq, type); })
> -    (if (innereq ? cst1 : cst0)
> +    (switch
> +     (if (innereq ? cst0 : cst1)
> +      { constant_boolean_node (!outereq, type); })
> +     (if (innereq ? cst1 : cst0)
> +      (with {
> +        tree utype = unsigned_type_for (TREE_TYPE (@0));
> +        tree ucst1 = build_one_cst (utype);
> +       }
> +       (if (!outereq)
> +        (gt (convert:utype @0) { ucst1; })
> +        (le (convert:utype @0) { ucst1; })
> +       )
> +      )
> +     )
>       (with {
> -       tree utype = unsigned_type_for (TREE_TYPE (@0));
> -       tree ucst1 = build_one_cst (utype);
> +       tree value = build_int_cst (TREE_TYPE (@0), !innereq);
>        }
> -      (if (!outereq)
> -       (gt (convert:utype @0) { ucst1; })
> -       (le (convert:utype @0) { ucst1; })
> +      (if (outereq)
> +       (eq @0 { value; })
> +       (ne @0 { value; })
>        )
>       )
>      )
> -    (if (innereq)
> -     (ne @0 { build_zero_cst (TREE_TYPE (@0)); }))
> -    (ne @0 { build_one_cst (TREE_TYPE (@0)); }))
>     )
>    )
>   )
> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c
> new file mode 100644
> index 00000000000..b22eb7781da
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c
> @@ -0,0 +1,51 @@
> +
> +#define func_name(outer,inner,cst) outer##inner##_##cst
> +#define func_name_v(outer,inner,cst) outer##inner##_##cst##_v
> +
> +#define func_decl(outer,inner,cst) \
> +int outer##inner##_##cst (int) __attribute__((noipa)); \
> +int outer##inner##_##cst (int a) { \
> +  return (a op_##inner cst) op_##outer a; \
> +} \
> +int outer##inner##_##cst##_v (int) __attribute__((noipa)); \
> +int outer##inner##_##cst##_v (volatile int a) { \
> +  return (a op_##inner cst) op_##outer a; \
> +}
> +
> +#define functions_n(outer, inner) \
> +func_decl(outer,inner,0) \
> +func_decl(outer,inner,1) \
> +func_decl(outer,inner,2)
> +
> +#define functions() \
> +functions_n(eq,eq) \
> +functions_n(eq,ne) \
> +functions_n(ne,eq) \
> +functions_n(ne,ne)
> +
> +#define op_ne !=
> +#define op_eq ==
> +
> +#define test(inner,outer,cst,arg) \
> +func_name_v (inner,outer,cst)(arg) != func_name(inner,outer,cst)(arg)
> +
> +functions()
> +
> +#define tests_n(inner,outer,arg) \
> +if (test(inner,outer,0,arg)) __builtin_abort(); \
> +if (test(inner,outer,1,arg)) __builtin_abort(); \
> +if (test(inner,outer,2,arg)) __builtin_abort();
> +
> +#define tests(arg) \
> +tests_n(eq,eq,arg) \
> +tests_n(eq,ne,arg) \
> +tests_n(ne,eq,arg) \
> +tests_n(ne,ne,arg)
> +
> +
> +int main()
> +{
> +  for(int n = -1; n <= 2; n++) {
> +    tests(n)
> +  }
> +}
> --
> 2.31.1
>

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

end of thread, other threads:[~2023-07-17  6:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-14 20:55 [PATCH] Fix PR 110666: `(a != 2) == a` produces wrong code Andrew Pinski
2023-07-17  6:20 ` 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).