From 8085e8e4f2e7dd92959856c1df09b9fae1fdae2f Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 May 2018 12:30:53 -0400
Subject: [PATCH 14/67] FIXME: start adding optimization records

---
 gcc/Makefile.in             |   1 +
 gcc/optimization-records.cc | 123 ++++++++++++++++++++++++++++++++++++++++++++
 gcc/optimization-records.h  |  32 ++++++++++++
 gcc/profile.c               |   2 +
 gcc/toplev.c                |   3 ++
 5 files changed, 161 insertions(+)
 create mode 100644 gcc/optimization-records.cc
 create mode 100644 gcc/optimization-records.h

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index b3c7d5d..26c0a40 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1426,6 +1426,7 @@ OBJS = \
 	optabs-libfuncs.o \
 	optabs-query.o \
 	optabs-tree.o \
+	optimization-records.o \
 	options-save.o \
 	opts-global.o \
 	passes.o \
diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
new file mode 100644
index 0000000..7ef5734
--- /dev/null
+++ b/gcc/optimization-records.cc
@@ -0,0 +1,123 @@
+/* Support for recording information about optimizations.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+
+/* FIXME: trim these.  */
+#include "backend.h"
+#include "rtl.h"
+#include "tree.h"
+#include "gimple.h"
+#include "cfghooks.h"
+#include "cgraph.h"
+#include "coverage.h"
+#include "diagnostic-core.h"
+#include "cfganal.h"
+#include "value-prof.h"
+#include "gimple-iterator.h"
+#include "tree-cfg.h"
+#include "dumpfile.h"
+#include "cfgloop.h"
+
+#include "profile.h"
+
+#include "json.h"
+
+/* FIXME.  */
+
+static json::object *
+location_as_json (location_t loc)
+{
+  json::object *obj = new json::object ();
+  obj->set ("file", new json::string (LOCATION_FILE (loc)));
+  obj->set ("line", new json::number (LOCATION_LINE (loc)));
+  obj->set ("column", new json::number (LOCATION_COLUMN (loc)));
+  return obj;
+}
+
+/* FIXME.  */
+
+static json::array *records;
+
+/* FIXME.  */
+
+void
+optimization_records_start ()
+{
+  // FIXME: conditionalize this on a var
+  records = new json::array ();
+}
+
+/* FIXME.  */
+
+void
+optimization_records_finish ()
+{
+  /* Bail immediately if recording not enabled.  */
+  if (!records)
+    return;
+
+  // FIXME: pick a better location
+  records->dump (stderr);
+  fprintf (stderr, "\n");
+  delete records;
+  records = NULL;
+}
+
+/* FIXME.  */
+
+void
+maybe_record_optimization (gimple *stmt, const char *msg)
+{
+  /* 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)
+    {
+      // TOOD: record the location (just caret for now)
+      // TODO: start/finish also?
+      obj->set ("location", location_as_json (loc));
+    }
+
+#if 0
+  if (current_function_decl)
+    {
+      const char *fnname = get_fnname_from_decl (current_function_decl);
+      obj->set ("function", new json::string (fnname));
+    }
+#endif
+
+  obj->set ("message", new json::string (msg));
+
+  // TODO: record the stmt->bb->count
+  obj->set ("count", new json::number (stmt->bb->count.to_gcov_type ()));
+  // TODO: record the quality info associated with the count?
+
+  records->append (obj);
+}
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
new file mode 100644
index 0000000..a189c42
--- /dev/null
+++ b/gcc/optimization-records.h
@@ -0,0 +1,32 @@
+/* Support for recording information about optimizations.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_OPTIMIZATION_RECORDS_H
+#define GCC_OPTIMIZATION_RECORDS_H
+
+/* FIXME.  */
+
+extern void optimization_records_start ();
+extern void optimization_records_finish ();
+
+extern void maybe_record_optimization (gimple *stmt, const char *msg);
+
+
+#endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
diff --git a/gcc/profile.c b/gcc/profile.c
index d380d1e..f1d3a8b 100644
--- a/gcc/profile.c
+++ b/gcc/profile.c
@@ -64,6 +64,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-cfg.h"
 #include "dumpfile.h"
 #include "cfgloop.h"
+#include "optimization-records.h"
 
 #include "profile.h"
 
@@ -1357,6 +1358,7 @@ branch_prob (void)
 	  gimple *stmt = gsi_stmt (gsi);
 	  remark (gimple_location (stmt), 0, "test remark; execution count=%li",
 		  stmt->bb->count.to_gcov_type ());
+	  maybe_record_optimization (stmt, "test remark");
 #if 0
   /* Expected number of executions: calculated in profile.c.  */
   profile_count count;
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 96b2a7c..ceabc42 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -84,6 +84,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-pass.h"
 #include "dumpfile.h"
 #include "ipa-fnsummary.h"
+#include "optimization-records.h"
 
 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
 #include "dbxout.h"
@@ -489,6 +490,7 @@ compile_file (void)
     lang_hooks.decls.post_compilation_parsing_cleanups ();
 
   fancy_dump_finish ();
+  optimization_records_finish ();
 
   if (seen_error ())
     return;
@@ -2095,6 +2097,7 @@ do_compile ()
       int i;
 
       fancy_dump_start ();
+      optimization_records_start ();
 
       timevar_start (TV_PHASE_SETUP);
 
-- 
1.8.5.3

