]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
middle-end/108625 - wrong folding due to misinterpreted !
authorRichard Biener <rguenther@suse.de>
Thu, 2 Feb 2023 10:09:26 +0000 (11:09 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 15 Mar 2023 09:07:16 +0000 (10:07 +0100)
The following fixes a problem with ! handling in genmatch which isn't
conservative enough when intermediate simplifications push to the
sequence but the final operation appears to just pick an existing
(but in this case newly defined in the sequence) operand.  The easiest
fix is to disallow adding to the sequence when processing !.

PR middle-end/108625
* genmatch.cc (expr::gen_transform): Also disallow resimplification
from pushing to lseq with force_leaf.
(dt_simplify::gen_1): Likewise.

* gcc.dg/pr108625.c: New testcase.

(cherry picked from commit 605d1297b91c2c7c23ccfe669e66dda5791d1f55)

gcc/genmatch.cc
gcc/testsuite/gcc.dg/pr108625.c [new file with mode: 0644]

index 2eda73008219132ddd682f231ea7e424b9b2e553..5b25fc8da86c4812b5b219a2aafd59e07bf6d856 100644 (file)
@@ -2526,7 +2526,8 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
       for (unsigned i = 0; i < ops.length (); ++i)
        fprintf (f, ", _o%d[%u]", depth, i);
       fprintf (f, ");\n");
-      fprintf_indent (f, indent, "tem_op.resimplify (lseq, valueize);\n");
+      fprintf_indent (f, indent, "tem_op.resimplify (%s, valueize);\n",
+                     !force_leaf ? "lseq" : "NULL");
       fprintf_indent (f, indent,
                      "_r%d = maybe_push_res_to_seq (&tem_op, %s);\n", depth,
                      !force_leaf ? "lseq" : "NULL");
@@ -3428,7 +3429,8 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
          if (!is_predicate)
            {
              fprintf_indent (f, indent,
-                             "res_op->resimplify (lseq, valueize);\n");
+                             "res_op->resimplify (%s, valueize);\n",
+                             !e->force_leaf ? "lseq" : "NULL");
              if (e->force_leaf)
                fprintf_indent (f, indent,
                                "if (!maybe_push_res_to_seq (res_op, NULL)) "
diff --git a/gcc/testsuite/gcc.dg/pr108625.c b/gcc/testsuite/gcc.dg/pr108625.c
new file mode 100644 (file)
index 0000000..03fc288
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1 -fdump-tree-optimized" } */
+
+unsigned char foo(int x)
+{
+  int t = -x;
+  unsigned char t1 = t;
+  unsigned char t2 = t;
+  /* We may not rewrite this as (unsigned char)(t - x).  */
+  return t1 + t2;
+}
+
+/* { dg-final { scan-tree-dump-times "x_" 1 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "x_" 1 "optimized" } } */