]> gcc.gnu.org Git - gcc.git/commitdiff
middle-end/104644 - recursion with bswap match.pd pattern
authorRichard Biener <rguenther@suse.de>
Wed, 23 Feb 2022 12:47:01 +0000 (13:47 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 23 Feb 2022 12:51:43 +0000 (13:51 +0100)
The following patch avoids infinite recursion during generic folding.
The (cmp (bswap @0) INTEGER_CST@1) simplification relies on
(bswap @1) actually being simplified, if it is not simplified, we just
move the bswap from one operand to the other and if @0 is also INTEGER_CST,
we apply the same rule next.

The reason why bswap @1 isn't folded to INTEGER_CST is that the INTEGER_CST
has TREE_OVERFLOW set on it and fold-const-call.cc predicate punts in
such cases:
static inline bool
integer_cst_p (tree t)
{
  return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
}
The patch uses ! modifier to ensure the bswap is simplified and
extends support to GENERIC by means of requiring !EXPR_P which
is not perfect but a conservative approximation.

2022-02-22  Richard Biener  <rguenther@suse.de>

PR tree-optimization/104644
* doc/match-and-simplify.texi: Amend ! documentation.
* genmatch.cc (expr::gen_transform): Code-generate ! support
for GENERIC.
(parser::parse_expr): Allow ! for GENERIC.
* match.pd (cmp (bswap @0) INTEGER_CST@1): Use ! modifier on
bswap.

* gcc.dg/pr104644.c: New test.

Co-Authored-by: Jakub Jelinek <jakub@redhat.com>
gcc/doc/match-and-simplify.texi
gcc/genmatch.cc
gcc/match.pd
gcc/testsuite/gcc.dg/pr104644.c [new file with mode: 0644]

index 63a73ae047cafbdb9194f08bd2f450466b42354b..055a5308e7dcf7a476cdea8cb2bf8f98570adaca 100644 (file)
@@ -374,8 +374,10 @@ for example
 
 which moves the outer @code{plus} operation to the inner arms
 of the @code{vec_cond} expression but only if the actual plus
-operations both simplify.  Note this is currently only supported
-for code generation targeting @code{GIMPLE}.
+operations both simplify.  Note that on @code{GENERIC} a simple
+operand means that the result satisfies @code{!EXPR_P} which
+can be limiting if the operation itself simplifies but the
+remaining operand is an (unrelated) expression.
 
 As intermediate conversions are often optional there is a way to
 avoid the need to repeat patterns both with and without such
index 97f6f00fa687c619e9ca5db620910b425c8d135d..2eda73008219132ddd682f231ea7e424b9b2e553 100644 (file)
@@ -2553,19 +2553,20 @@ expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
        fprintf_indent (f, indent, "_r%d = fold_build%d_loc (loc, %s, %s",
                        depth, ops.length(), opr_name, type);
       else
-       {
-         fprintf_indent (f, indent, "{\n");
-         fprintf_indent (f, indent, "  _r%d = maybe_build_call_expr_loc (loc, "
-                         "%s, %s, %d", depth, opr_name, type, ops.length());
-       }
+       fprintf_indent (f, indent, "_r%d = maybe_build_call_expr_loc (loc, "
+                       "%s, %s, %d", depth, opr_name, type, ops.length());
       for (unsigned i = 0; i < ops.length (); ++i)
        fprintf (f, ", _o%d[%u]", depth, i);
       fprintf (f, ");\n");
       if (opr->kind != id_base::CODE)
        {
-         fprintf_indent (f, indent, "  if (!_r%d)\n", depth);
-         fprintf_indent (f, indent, "    goto %s;\n", fail_label);
-         fprintf_indent (f, indent, "}\n");
+         fprintf_indent (f, indent, "if (!_r%d)\n", depth);
+         fprintf_indent (f, indent, "  goto %s;\n", fail_label);
+       }
+      if (force_leaf)
+       {
+         fprintf_indent (f, indent, "if (EXPR_P (_r%d))\n", depth);
+         fprintf_indent (f, indent, "  goto %s;\n", fail_label);
        }
       if (*opr == CONVERT_EXPR)
        {
@@ -4297,9 +4298,6 @@ parser::parse_expr ()
       && token->type == CPP_NOT
       && !(token->flags & PREV_WHITE))
     {
-      if (!gimple)
-       fatal_at (token, "forcing simplification to a leaf is not supported "
-                 "for GENERIC");
       eat_token (CPP_NOT);
       e->force_leaf = true;
     }
index cad61848daada9cc3ceb600084285dc4c9b18f4f..cf78a11ddd4aacaec8e9d45c9690e26b1704302e 100644 (file)
@@ -3962,7 +3962,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (simplify
    (cmp (bswap @0) INTEGER_CST@1)
    (with { tree ctype = TREE_TYPE (@1); }
-    (cmp (convert:ctype @0) (bswap @1)))))
+    (cmp (convert:ctype @0) (bswap! @1)))))
  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
  (simplify
   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
diff --git a/gcc/testsuite/gcc.dg/pr104644.c b/gcc/testsuite/gcc.dg/pr104644.c
new file mode 100644 (file)
index 0000000..70bf3a4
--- /dev/null
@@ -0,0 +1,9 @@
+/* PR tree-optimization/104644 */
+/* { dg-do compile } */
+/* { dg-options "-Wno-overflow" } */
+
+int
+foo (void)
+{
+  return __builtin_bswap16 (1.31072e+5f) != (signed char) 1.31072e+5f;
+}
This page took 0.087656 seconds and 5 git commands to generate.