genmatch: Emit debug message right before "return x" instead of earlier

Message ID 20230526025838.2259892-1-apinski@marvell.com
State Committed
Commit 497e8e2420e3fc0209ae240f61050d0257023d6b
Headers
Series genmatch: Emit debug message right before "return x" instead of earlier |

Commit Message

Andrew Pinski May 26, 2023, 2:58 a.m. UTC
  This is based on the review of https://gcc.gnu.org/pipermail/gcc-patches/2023-May/619342.html .
Instead of emitting debug message even if we don't apply a pattern, this fixes the issue
by only emitting it if it the pattern finally succeeded.

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

gcc/ChangeLog:

	* genmatch.cc (emit_debug_printf): New function.
	(dt_simplify::gen_1): Emit printf into the code
	before the `return true` or returning the folded result
	instead of emitting it always.
---
 gcc/genmatch.cc | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
  

Comments

Richard Biener May 26, 2023, 6:58 a.m. UTC | #1
On Fri, May 26, 2023 at 4:59 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This is based on the review of https://gcc.gnu.org/pipermail/gcc-patches/2023-May/619342.html .
> Instead of emitting debug message even if we don't apply a pattern, this fixes the issue
> by only emitting it if it the pattern finally succeeded.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK

Thanks,
Richard.

> gcc/ChangeLog:
>
>         * genmatch.cc (emit_debug_printf): New function.
>         (dt_simplify::gen_1): Emit printf into the code
>         before the `return true` or returning the folded result
>         instead of emitting it always.
> ---
>  gcc/genmatch.cc | 33 ++++++++++++++++++++++-----------
>  1 file changed, 22 insertions(+), 11 deletions(-)
>
> diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
> index 177c13d87cb..bd6ce3a28f8 100644
> --- a/gcc/genmatch.cc
> +++ b/gcc/genmatch.cc
> @@ -3359,6 +3359,21 @@ dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
>      }
>  }
>
> +/* Emit a fprintf to the debug file to the file F, with the INDENT from
> +   either the RESULT location or the S's match location if RESULT is null. */
> +static void
> +emit_debug_printf (FILE *f, int indent, class simplify *s, operand *result)
> +{
> +  fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> +          "fprintf (dump_file, \"%s ",
> +          s->kind == simplify::SIMPLIFY
> +          ? "Applying pattern" : "Matching expression");
> +  fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
> +  output_line_directive (f,
> +                        result ? result->location : s->match->location, true,
> +                        true);
> +  fprintf (f, ", __FILE__, __LINE__);\n");
> +}
>
>  /* Generate code for the '(if ...)', '(with ..)' and actual transform
>     step of a '(simplify ...)' or '(match ...)'.  This handles everything
> @@ -3488,21 +3503,12 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>        needs_label = true;
>      }
>
> -  fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
> -          "fprintf (dump_file, \"%s ",
> -          s->kind == simplify::SIMPLIFY
> -          ? "Applying pattern" : "Matching expression");
> -  fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
> -  output_line_directive (f,
> -                        result ? result->location : s->match->location, true,
> -                        true);
> -  fprintf (f, ", __FILE__, __LINE__);\n");
> -
>    fprintf_indent (f, indent, "{\n");
>    indent += 2;
>    if (!result)
>      {
>        /* If there is no result then this is a predicate implementation.  */
> +      emit_debug_printf (f, indent, s, result);
>        fprintf_indent (f, indent, "return true;\n");
>      }
>    else if (gimple)
> @@ -3593,6 +3599,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>         }
>        else
>         gcc_unreachable ();
> +      emit_debug_printf (f, indent, s, result);
>        fprintf_indent (f, indent, "return true;\n");
>      }
>    else /* GENERIC */
> @@ -3646,7 +3653,10 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>                                         &cinfo, indexes);
>             }
>           if (is_predicate)
> -           fprintf_indent (f, indent, "return true;\n");
> +           {
> +             emit_debug_printf (f, indent, s, result);
> +             fprintf_indent (f, indent, "return true;\n");
> +           }
>           else
>             {
>               fprintf_indent (f, indent, "tree _r;\n");
> @@ -3712,6 +3722,7 @@ dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
>                                   i);
>                 }
>             }
> +         emit_debug_printf (f, indent, s, result);
>           fprintf_indent (f, indent, "return _r;\n");
>         }
>      }
> --
> 2.31.1
>
  

Patch

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index 177c13d87cb..bd6ce3a28f8 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -3359,6 +3359,21 @@  dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
     }
 }
 
+/* Emit a fprintf to the debug file to the file F, with the INDENT from
+   either the RESULT location or the S's match location if RESULT is null. */
+static void
+emit_debug_printf (FILE *f, int indent, class simplify *s, operand *result)
+{
+  fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
+	   "fprintf (dump_file, \"%s ",
+	   s->kind == simplify::SIMPLIFY
+	   ? "Applying pattern" : "Matching expression");
+  fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
+  output_line_directive (f,
+			 result ? result->location : s->match->location, true,
+			 true);
+  fprintf (f, ", __FILE__, __LINE__);\n");
+}
 
 /* Generate code for the '(if ...)', '(with ..)' and actual transform
    step of a '(simplify ...)' or '(match ...)'.  This handles everything
@@ -3488,21 +3503,12 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
       needs_label = true;
     }
 
-  fprintf_indent (f, indent, "if (UNLIKELY (debug_dump)) "
-	   "fprintf (dump_file, \"%s ",
-	   s->kind == simplify::SIMPLIFY
-	   ? "Applying pattern" : "Matching expression");
-  fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
-  output_line_directive (f,
-			 result ? result->location : s->match->location, true,
-			 true);
-  fprintf (f, ", __FILE__, __LINE__);\n");
-
   fprintf_indent (f, indent, "{\n");
   indent += 2;
   if (!result)
     {
       /* If there is no result then this is a predicate implementation.  */
+      emit_debug_printf (f, indent, s, result);
       fprintf_indent (f, indent, "return true;\n");
     }
   else if (gimple)
@@ -3593,6 +3599,7 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
 	}
       else
 	gcc_unreachable ();
+      emit_debug_printf (f, indent, s, result);
       fprintf_indent (f, indent, "return true;\n");
     }
   else /* GENERIC */
@@ -3646,7 +3653,10 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
 					&cinfo, indexes);
 	    }
 	  if (is_predicate)
-	    fprintf_indent (f, indent, "return true;\n");
+	    {
+	      emit_debug_printf (f, indent, s, result);
+	      fprintf_indent (f, indent, "return true;\n");
+	    }
 	  else
 	    {
 	      fprintf_indent (f, indent, "tree _r;\n");
@@ -3712,6 +3722,7 @@  dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
 				  i);
 		}
 	    }
+	  emit_debug_printf (f, indent, s, result);
 	  fprintf_indent (f, indent, "return _r;\n");
 	}
     }