From a4bc55fd1a5c41de47c1e67e5a2916a08e523f0a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 May 2018 20:07:15 -0400
Subject: [PATCH 19/67] FIXME: more on optimization records

---
 gcc/ipa-inline.c            | 11 +++++++++++
 gcc/optimization-records.cc | 32 +++++++++++++++++++++++++-------
 gcc/optimization-records.h  |  4 +++-
 3 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/gcc/ipa-inline.c b/gcc/ipa-inline.c
index b7f213f..4693341 100644
--- a/gcc/ipa-inline.c
+++ b/gcc/ipa-inline.c
@@ -120,6 +120,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stringpool.h"
 #include "attribs.h"
 #include "asan.h"
+#include "optimization-records.h"
 
 typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
 typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
@@ -2167,6 +2168,11 @@ flatten_function (struct cgraph_node *node, bool early)
 	fprintf (dump_file, " Inlining %s into %s.\n",
 		 xstrdup_for_dump (callee->name ()),
 		 xstrdup_for_dump (e->caller->name ()));
+      // FIXME: need a guard for when things are expensive:
+      if (1)
+	maybe_record_optimization (e->call_stmt, "inlining %s into %s",
+				   xstrdup_for_dump (callee->name ()),
+				   xstrdup_for_dump (e->caller->name ()));
       orig_callee = callee;
       inline_call (e, true, NULL, NULL, false);
       if (e->callee != orig_callee)
@@ -2677,6 +2683,11 @@ early_inline_small_functions (struct cgraph_node *node)
 	fprintf (dump_file, " Inlining %s into %s.\n",
 		 xstrdup_for_dump (callee->name ()),
 		 xstrdup_for_dump (e->caller->name ()));
+      // FIXME: need a guard for when things are expensive:
+      if (1)
+	maybe_record_optimization (e->call_stmt, "inlining %s into %s",
+				   xstrdup_for_dump (callee->name ()),
+				   xstrdup_for_dump (e->caller->name ()));
       inline_call (e, true, NULL, NULL, false);
       inlined = true;
     }
diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
index e01bad0..e8df180 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -40,7 +40,9 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "profile.h"
 #include "output.h"
+#include "tree-pass.h"
 
+#include "optimization-records.h"
 #include "json.h"
 
 /* FIXME.  */
@@ -91,9 +93,18 @@ optimization_records_finish ()
   if (!records)
     return;
 
-  // FIXME: pick a better location
-  records->dump (stderr);
-  fprintf (stderr, "\n");
+  char *filename = concat (dump_base_name, ".opt-record.json", NULL);
+  FILE *outfile = fopen (filename, "w");
+  if (outfile)
+    {
+      records->dump (outfile);
+      fclose (outfile);
+    }
+  else
+    error_at (UNKNOWN_LOCATION, "unable to write optimization records to %qs",
+	      filename); // FIXME: more info?
+  free (filename);
+
   delete records;
   records = NULL;
 }
@@ -101,17 +112,15 @@ optimization_records_finish ()
 /* FIXME.  */
 
 void
-maybe_record_optimization (gimple *stmt, const char *msg)
+maybe_record_optimization (gimple *stmt, const char *fmt, ...)
 {
   /* Bail immediately if recording not enabled.  */
   if (!records)
     return;
 
   json::object *obj = new json::object ();
-#if 0
   if (current_pass)
     obj->set ("pass", new json::string (current_pass->name));
-#endif
   location_t loc = gimple_location (stmt);
   if (loc != UNKNOWN_LOCATION)
     {
@@ -126,8 +135,17 @@ maybe_record_optimization (gimple *stmt, const char *msg)
       obj->set ("function", new json::string (fnname));
     }
 
+  /* Formatted output.  */
+  va_list ap;
+  va_start (ap, fmt);
+  char *msg;
+  vasprintf (&msg, fmt, ap);
   obj->set ("message", new json::string (msg));
-  obj->set ("count", profile_count_to_json (stmt->bb->count));
+  free (msg);
+  va_end (ap);
+
+  if (stmt->bb->count.initialized_p ())
+    obj->set ("count", profile_count_to_json (stmt->bb->count));
 
   records->append (obj);
 }
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
index a189c42..f49b57a 100644
--- a/gcc/optimization-records.h
+++ b/gcc/optimization-records.h
@@ -26,7 +26,9 @@ along with GCC; see the file COPYING3.  If not see
 extern void optimization_records_start ();
 extern void optimization_records_finish ();
 
-extern void maybe_record_optimization (gimple *stmt, const char *msg);
+// FIXME: param name?  i18n?
 
+extern void maybe_record_optimization (gimple *stmt, const char *fmt, ...)
+  ATTRIBUTE_PRINTF_2;
 
 #endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
-- 
1.8.5.3

