]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
For the `-A CMP -B -> B CMP A` pattern allow EQ/NE for all integer types
authorAndrew Pinski <apinski@marvell.com>
Tue, 6 Jun 2023 02:12:43 +0000 (19:12 -0700)
committerAndrew Pinski <apinski@marvell.com>
Wed, 7 Jun 2023 02:57:18 +0000 (19:57 -0700)
I noticed while looking at some code generation issue, that forwprop
was not handling `-a == 0` for unsigned types and I was confused why
it was not.
r6-1814-g66e1cacf608045 removed these from fold because they
were supposed to be already handled by the match.pd patterns
but it was missed that the match.pd patterns checked
TYPE_OVERFLOW_UNDEFINED while fold didn't do that for NE/EQ.
This patch removes the restriction on NE/EQ on TYPE_OVERFLOW_UNDEFINED.

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

gcc/ChangeLog:

PR tree-optimization/110134
* match.pd (-A CMP -B -> B CMP A): Allow EQ/NE for all integer
types.
(-A CMP CST -> B CMP (-CST)): Likewise.

gcc/testsuite/ChangeLog:

PR tree-optimization/110134
* gcc.dg/tree-ssa/negneq-1.c: New test.
* gcc.dg/tree-ssa/negneq-2.c: New test.
* gcc.dg/tree-ssa/negneq-3.c: New test.
* gcc.dg/tree-ssa/negneq-4.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c [new file with mode: 0644]

index 941f1be9f8edd7bc6f9db288b1c650072454b0c7..dc36927cd0f045eb80c416d88ee25115c75df70e 100644 (file)
@@ -5919,13 +5919,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cmp (negate @0) (negate @1))
   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-          && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
+          && (cmp == EQ_EXPR
+              || cmp == NE_EXPR
+              || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
    (scmp @0 @1)))
  (simplify
   (cmp (negate @0) CONSTANT_CLASS_P@1)
   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-          && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
+          && (cmp == EQ_EXPR
+              || cmp == NE_EXPR
+              || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
     (if (tem && !TREE_OVERFLOW (tem))
      (scmp @0 { tem; }))))))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c
new file mode 100644 (file)
index 0000000..94ff57d
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a)
+{
+  a = -a;
+  return a != 0;
+}
+int fs(signed a)
+{
+  a = -a;
+  return a != 0;
+}
+
+/* We should have optimized out the a = -a; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c
new file mode 100644 (file)
index 0000000..9a3bb48
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a)
+{
+  a = -a;
+  return a == 1;
+}
+int fs(signed a)
+{
+  a = -a;
+  return a == 1;
+}
+
+/* We should have optimized out the a = -a; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c
new file mode 100644 (file)
index 0000000..14546a6
--- /dev/null
@@ -0,0 +1,20 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a, unsigned b)
+{
+  a = -a;
+  b = -b;
+  return a == b;
+}
+int fs(signed a, signed b)
+{
+  a = -a;
+  b = -b;
+  return a == b;
+}
+
+/* We should have optimized out the a = -; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "= -b" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c
new file mode 100644 (file)
index 0000000..1b66854
--- /dev/null
@@ -0,0 +1,20 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a, unsigned b)
+{
+  a = -a;
+  b = -b;
+  return a != b;
+}
+int fs(signed a, signed b)
+{
+  a = -a;
+  b = -b;
+  return a != b;
+}
+
+/* We should have optimized out the a = -; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "= -b" "optimized" } } */