public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Trivial cleanups for genmatch
@ 2023-05-08 18:13 Alexander Monakov
  2023-05-08 18:13 ` [PATCH 1/3] genmatch: clean up emit_func Alexander Monakov
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Alexander Monakov @ 2023-05-08 18:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: Alexander Monakov

I'm trying to study match.pd/genmatch with the eventual goal of
improving match-and-simplify code generation. Here's some trivial
cleanups for the recent refactoring in the meantime.

Alexander Monakov (3):
  genmatch: clean up emit_func
  genmatch: clean up showUsage
  genmatch: fixup get_out_file

 gcc/genmatch.cc | 159 ++++++++++++++++++++++++------------------------
 1 file changed, 79 insertions(+), 80 deletions(-)

-- 
2.39.2


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

* [PATCH 1/3] genmatch: clean up emit_func
  2023-05-08 18:13 [PATCH 0/3] Trivial cleanups for genmatch Alexander Monakov
@ 2023-05-08 18:13 ` Alexander Monakov
  2023-05-08 18:13 ` [PATCH 2/3] genmatch: clean up showUsage Alexander Monakov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Alexander Monakov @ 2023-05-08 18:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: Alexander Monakov

Eliminate boolean parameters of emit_func. The first ('open') just
prints 'extern' to generated header, which is unnecessary. Introduce a
separate function to use when finishing a declaration in place of the
second ('close').

Rename emit_func to 'fp_decl' (matching 'fprintf' in length) to unbreak
indentation in several places.

Reshuffle emitted line breaks in a few places to make generated
declarations less ugly.

gcc/ChangeLog:

	* genmatch.cc (header_file): Make static.
	(emit_func): Rename to...
	(fp_decl): ... this.  Adjust all uses.
	(fp_decl_done): New function.  Use it...
	(decision_tree::gen): ... here and...
	(write_predicate): ... here.
	(main): Adjust.
---
 gcc/genmatch.cc | 97 ++++++++++++++++++++++++++-----------------------
 1 file changed, 52 insertions(+), 45 deletions(-)

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index c593814871..d5e56e2d68 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -183,31 +183,37 @@ fprintf_indent (FILE *f, unsigned int indent, const char *format, ...)
   va_end (ap);
 }
 
-/* Like fprintf, but print to two files, one header one C implementation.  */
-FILE *header_file = NULL;
+/* Secondary stream for fp_decl.  */
+static FILE *header_file;
 
+/* Start or continue emitting a declaration in fprintf-like manner,
+   printing both to F and global header_file, if non-null.  */
 static void
 #if GCC_VERSION >= 4001
-__attribute__((format (printf, 4, 5)))
+__attribute__((format (printf, 2, 3)))
 #endif
-emit_func (FILE *f, bool open, bool close, const char *format, ...)
+fp_decl (FILE *f, const char *format, ...)
 {
-  va_list ap1, ap2;
-  if (header_file != NULL)
-    {
-      if (open)
-	fprintf (header_file, "extern ");
-      va_start (ap2, format);
-      vfprintf (header_file, format, ap2);
-      va_end (ap2);
-      if (close)
-	fprintf (header_file, ";\n");
-    }
+  va_list ap;
+  va_start (ap, format);
+  vfprintf (f, format, ap);
+  va_end (ap);
 
-  va_start (ap1, format);
-  vfprintf (f, format, ap1);
-  va_end (ap1);
-  fputc ('\n', f);
+  if (!header_file)
+    return;
+
+  va_start (ap, format);
+  vfprintf (header_file, format, ap);
+  va_end (ap);
+}
+
+/* Finish a declaration being emitted by fp_decl.  */
+static void
+fp_decl_done (FILE *f, const char *trailer)
+{
+  fprintf (f, "%s\n", trailer);
+  if (header_file)
+    fprintf (header_file, "%s;", trailer);
 }
 
 static void
@@ -3924,35 +3930,35 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
       s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
 			    fcnt++);
       if (gimple)
-	emit_func (f, true, false, "\nbool\n"
+	fp_decl (f, "\nbool\n"
 		 "%s (gimple_match_op *res_op, gimple_seq *seq,\n"
 		 "                 tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
 		 "                 const tree ARG_UNUSED (type), tree *ARG_UNUSED "
-		 "(captures)\n",
+		 "(captures)",
 		 s->fname);
       else
 	{
-	  emit_func (f, true, false, "\ntree\n"
+	  fp_decl (f, "\ntree\n"
 		   "%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
 		   (*iter).second->fname);
 	  for (unsigned i = 0;
 	       i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
-	    emit_func (f, false, false, " tree ARG_UNUSED (_p%d),", i);
-	  emit_func (f, false, false, " tree *captures\n");
+	    fp_decl (f, " tree ARG_UNUSED (_p%d),", i);
+	  fp_decl (f, " tree *captures");
 	}
       for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
 	{
 	  if (! s->s->s->for_subst_vec[i].first->used)
 	    continue;
 	  if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second))
-	    emit_func (f, false, false, ", const enum tree_code ARG_UNUSED (%s)",
+	    fp_decl (f, ",\n const enum tree_code ARG_UNUSED (%s)",
 		     s->s->s->for_subst_vec[i].first->id);
 	  else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second))
-	    emit_func (f, false, false, ", const combined_fn ARG_UNUSED (%s)",
+	    fp_decl (f, ",\n const combined_fn ARG_UNUSED (%s)",
 		     s->s->s->for_subst_vec[i].first->id);
 	}
 
-      emit_func (f, false, true, ")");
+      fp_decl_done (f, ")");
       fprintf (f, "{\n");
       fprintf_indent (f, 2, "const bool debug_dump = "
 			    "dump_file && (dump_flags & TDF_FOLDING);\n");
@@ -3988,22 +3994,22 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 	  FILE *f = get_out_file (files);
 
 	  if (gimple)
-	    emit_func (f, true, false,"\nbool\n"
+	    fp_decl (f, "\nbool\n"
 		     "gimple_simplify_%s (gimple_match_op *res_op,"
 		     " gimple_seq *seq,\n"
 		     "                 tree (*valueize)(tree) "
 		     "ATTRIBUTE_UNUSED,\n"
 		     "                 code_helper ARG_UNUSED (code), tree "
-		     "ARG_UNUSED (type)\n",
+		     "ARG_UNUSED (type)",
 		     e->operation->id);
 	  else
-	    emit_func (f, true, false, "\ntree\n"
+	    fp_decl (f, "\ntree\n"
 		     "generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
 		     "tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
 		     e->operation->id);
 	  for (unsigned i = 0; i < n; ++i)
-	    emit_func (f, false, false,", tree _p%d", i);
-	  emit_func (f, false, true, ")");
+	    fp_decl (f, ", tree _p%d", i);
+	  fp_decl_done (f, ")");
 	  fprintf (f, "{\n");
 	  fprintf_indent (f, 2, "const bool debug_dump = "
 				"dump_file && (dump_flags & TDF_FOLDING);\n");
@@ -4025,17 +4031,17 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 	  FILE *f = get_out_file (files);
 
 	  if (gimple)
-	    emit_func (f, true, false, "\nbool\n"
+	    fp_decl (f, "\nbool\n"
 			"gimple_simplify (gimple_match_op*, gimple_seq*,\n"
 			"                 tree (*)(tree), code_helper,\n"
 			"                 const tree");
 	  else
-	    emit_func (f, true, false,"\ntree\n"
+	    fp_decl (f, "\ntree\n"
 			"generic_simplify (location_t, enum tree_code,\n"
 			"                  const tree");
 	  for (unsigned i = 0; i < n; ++i)
-	    emit_func (f, false, false, ", tree");
-	  emit_func (f, false, true, ")");
+	    fp_decl (f, ", tree");
+	  fp_decl_done (f, ")");
 	  fprintf (f, "{\n");
 	  if (gimple)
 	    fprintf (f, "  return false;\n");
@@ -4052,17 +4058,17 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
       /* Then generate the main entry with the outermost switch and
          tail-calls to the split-out functions.  */
       if (gimple)
-	emit_func (f, true, false, "\nbool\n"
+	fp_decl (f, "\nbool\n"
 		 "gimple_simplify (gimple_match_op *res_op, gimple_seq *seq,\n"
 		 "                 tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
 		 "                 code_helper code, const tree type");
       else
-	emit_func (f, true, false, "\ntree\n"
+	fp_decl (f, "\ntree\n"
 		 "generic_simplify (location_t loc, enum tree_code code, "
 		 "const tree type ATTRIBUTE_UNUSED");
       for (unsigned i = 0; i < n; ++i)
-	emit_func (f, false, false, ", tree _p%d", i);
-      emit_func (f, false, true, ")");
+	fp_decl (f, ", tree _p%d", i);
+      fp_decl_done (f, ")");
       fprintf (f, "{\n");
 
       if (gimple)
@@ -4117,10 +4123,11 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 void
 write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
 {
-  emit_func (f, true, true, "\nbool\n%s%s (tree t%s%s)",
-		gimple ? "gimple_" : "tree_", p->id,
-		p->nargs > 0 ? ", tree *res_ops" : "",
-		gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
+  fp_decl (f, "\nbool\n%s%s (tree t%s%s)",
+	   gimple ? "gimple_" : "tree_", p->id,
+	   p->nargs > 0 ? ", tree *res_ops" : "",
+	   gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
+  fp_decl_done (f, "");
   fprintf (f, "{\n");
   /* Conveniently make 'type' available.  */
   fprintf_indent (f, 2, "const tree type = TREE_TYPE (t);\n");
@@ -5484,7 +5491,7 @@ main (int argc, char **argv)
 
   if (header_file)
     {
-      fprintf (header_file, "#endif /* GCC_GIMPLE_MATCH_AUTO_H.  */\n");
+      fprintf (header_file, "\n#endif /* GCC_GIMPLE_MATCH_AUTO_H.  */\n");
       fclose (header_file);
     }
 
-- 
2.39.2


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

* [PATCH 2/3] genmatch: clean up showUsage
  2023-05-08 18:13 [PATCH 0/3] Trivial cleanups for genmatch Alexander Monakov
  2023-05-08 18:13 ` [PATCH 1/3] genmatch: clean up emit_func Alexander Monakov
@ 2023-05-08 18:13 ` Alexander Monakov
  2023-05-08 18:13 ` [PATCH 3/3] genmatch: fixup get_out_file Alexander Monakov
  2023-05-08 18:31 ` [PATCH 0/3] Trivial cleanups for genmatch Richard Biener
  3 siblings, 0 replies; 5+ messages in thread
From: Alexander Monakov @ 2023-05-08 18:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: Alexander Monakov

Display usage more consistently and get rid of camelCase.

gcc/ChangeLog:

	* genmatch.cc (showUsage): Reimplement as ...
	(usage): ...this.  Adjust all uses.
	(main): Print usage when no arguments.  Add missing 'return 1'.
---
 gcc/genmatch.cc | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index d5e56e2d68..baf93855a6 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -5301,13 +5301,12 @@ round_alloc_size (size_t s)
 /* Construct and display the help menu.  */
 
 static void
-showUsage ()
+usage ()
 {
-  fprintf (stderr, "Usage: genmatch [--gimple] [--generic] "
-		   "[--header=<filename>] [--include=<filename>] [-v[v]] input "
-		   "[<outputfile>...]\n");
-  fprintf (stderr, "\nWhen more then one outputfile is specified --header "
-		   "is required.\n");
+  const char *usage = "Usage:\n"
+    " %s [--gimple|--generic] [-v[v]] <input>\n"
+    " %s [options] [--include=FILE] --header=FILE <input> <output>...\n";
+  fprintf (stderr, usage, progname, progname);
 }
 
 /* Write out the correct include to the match-head fle containing the helper
@@ -5332,9 +5331,6 @@ main (int argc, char **argv)
 
   progname = "genmatch";
 
-  if (argc < 2)
-    return 1;
-
   bool gimple = true;
   char *s_header_file = NULL;
   char *s_include_file = NULL;
@@ -5359,14 +5355,17 @@ main (int argc, char **argv)
 	files.safe_push (argv[i]);
       else
 	{
-	  showUsage ();
+	  usage ();
 	  return 1;
 	}
     }
 
   /* Validate if the combinations are valid.  */
   if ((files.length () > 1 && !s_header_file) || files.is_empty ())
-    showUsage ();
+    {
+      usage ();
+      return 1;
+    }
 
   if (!s_include_file)
     s_include_file = s_header_file;
-- 
2.39.2


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

* [PATCH 3/3] genmatch: fixup get_out_file
  2023-05-08 18:13 [PATCH 0/3] Trivial cleanups for genmatch Alexander Monakov
  2023-05-08 18:13 ` [PATCH 1/3] genmatch: clean up emit_func Alexander Monakov
  2023-05-08 18:13 ` [PATCH 2/3] genmatch: clean up showUsage Alexander Monakov
@ 2023-05-08 18:13 ` Alexander Monakov
  2023-05-08 18:31 ` [PATCH 0/3] Trivial cleanups for genmatch Richard Biener
  3 siblings, 0 replies; 5+ messages in thread
From: Alexander Monakov @ 2023-05-08 18:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: Alexander Monakov

get_out_file did not follow the coding conventions (mixing three-space
and two-space indentation, missing linebreak before function name).

Take that as an excuse to reimplement it in a more terse manner and
rename as 'choose_output', which is hopefully more descriptive.

gcc/ChangeLog:

	* genmatch.cc (get_out_file): Make static and rename to ...
	(choose_output): ... this. Reimplement. Update all uses ...
	(decision_tree::gen): ... here and ...
	(main): ... here.
---
 gcc/genmatch.cc | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)

diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index baf93855a6..177c13d87c 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -255,28 +255,21 @@ output_line_directive (FILE *f, location_t location,
 
 #define SIZED_BASED_CHUNKS 1
 
-int current_file = 0;
-FILE *get_out_file (vec <FILE *> &parts)
+static FILE *
+choose_output (const vec<FILE *> &parts)
 {
 #ifdef SIZED_BASED_CHUNKS
-   if (parts.length () == 1)
-     return parts[0];
-
-   FILE *f = NULL;
-   long min = 0;
-   /* We've started writing all the files at pos 0, so ftell is equivalent
-      to the size and should be much faster.  */
-   for (unsigned i = 0; i < parts.length (); i++)
-     {
-	long res = ftell (parts[i]);
-	if (!f || res < min)
-	  {
-	    min = res;
-	    f = parts[i];
-	  }
-     }
-  return f;
+  FILE *shortest = NULL;
+  long min = 0;
+  for (FILE *part : parts)
+    {
+      long len = ftell (part);
+      if (!shortest || min > len)
+	shortest = part, min = len;
+    }
+  return shortest;
 #else
+  static int current_file;
   return parts[current_file++ % parts.length ()];
 #endif
 }
@@ -3924,7 +3917,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 	}
 
       /* Cycle the file buffers.  */
-      FILE *f = get_out_file (files);
+      FILE *f = choose_output (files);
 
       /* Generate a split out function with the leaf transform code.  */
       s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
@@ -3991,7 +3984,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 
 
 	  /* Cycle the file buffers.  */
-	  FILE *f = get_out_file (files);
+	  FILE *f = choose_output (files);
 
 	  if (gimple)
 	    fp_decl (f, "\nbool\n"
@@ -4028,7 +4021,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 	{
 
 	  /* Cycle the file buffers.  */
-	  FILE *f = get_out_file (files);
+	  FILE *f = choose_output (files);
 
 	  if (gimple)
 	    fp_decl (f, "\nbool\n"
@@ -4053,7 +4046,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
 
 
       /* Cycle the file buffers.  */
-      FILE *f = get_out_file (files);
+      FILE *f = choose_output (files);
 
       /* Then generate the main entry with the outermost switch and
          tail-calls to the split-out functions.  */
@@ -5461,7 +5454,7 @@ main (int argc, char **argv)
 	dt.print (stderr);
 
       /* Cycle the file buffers.  */
-      FILE *f = get_out_file (parts);
+      FILE *f = choose_output (parts);
 
       write_predicate (f, pred, dt, gimple);
     }
-- 
2.39.2


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

* Re: [PATCH 0/3] Trivial cleanups for genmatch
  2023-05-08 18:13 [PATCH 0/3] Trivial cleanups for genmatch Alexander Monakov
                   ` (2 preceding siblings ...)
  2023-05-08 18:13 ` [PATCH 3/3] genmatch: fixup get_out_file Alexander Monakov
@ 2023-05-08 18:31 ` Richard Biener
  3 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2023-05-08 18:31 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: gcc-patches



> Am 08.05.2023 um 20:14 schrieb Alexander Monakov via Gcc-patches <gcc-patches@gcc.gnu.org>:
> 
> I'm trying to study match.pd/genmatch with the eventual goal of
> improving match-and-simplify code generation. Here's some trivial
> cleanups for the recent refactoring in the meantime.
> 
> Alexander Monakov (3):
>  genmatch: clean up emit_func
>  genmatch: clean up showUsage
>  genmatch: fixup get_out_file

Ok for all.

Richard 


> gcc/genmatch.cc | 159 ++++++++++++++++++++++++------------------------
> 1 file changed, 79 insertions(+), 80 deletions(-)
> 
> -- 
> 2.39.2
> 

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

end of thread, other threads:[~2023-05-08 18:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-08 18:13 [PATCH 0/3] Trivial cleanups for genmatch Alexander Monakov
2023-05-08 18:13 ` [PATCH 1/3] genmatch: clean up emit_func Alexander Monakov
2023-05-08 18:13 ` [PATCH 2/3] genmatch: clean up showUsage Alexander Monakov
2023-05-08 18:13 ` [PATCH 3/3] genmatch: fixup get_out_file Alexander Monakov
2023-05-08 18:31 ` [PATCH 0/3] Trivial cleanups for genmatch 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).