From 2eccc156f37d1398b87694268757488027ba45a8 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 May 2018 09:47:35 -0400
Subject: [PATCH 08/83] FIXME: start adding fancy dump

---
 gcc/dumpfile.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gcc/dumpfile.h |   7 ++++
 gcc/passes.c   |   4 +++
 gcc/toplev.c   |   4 +++
 test.c         |  17 +++++++++
 5 files changed, 141 insertions(+)
 create mode 100644 test.c

diff --git a/gcc/dumpfile.c b/gcc/dumpfile.c
index 0acc7a9..6943613 100644
--- a/gcc/dumpfile.c
+++ b/gcc/dumpfile.c
@@ -29,6 +29,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "profile-count.h"
 #include "tree-cfg.h"
 #include "langhooks.h"
+#include "json.h"
+#include "tree-pass.h"
+#include "output.h"
 
 /* If non-NULL, return one past-the-end of the matching SUBPART of
    the WHOLE string.  */
@@ -1057,3 +1060,109 @@ enable_rtl_dump_file (void)
 			    NULL);
   return num_enabled > 0;
 }
+
+/* FIXME.  */
+
+class fancy_dump
+{
+ public:
+  fancy_dump ();
+  ~fancy_dump ();
+
+  void start_pass (opt_pass *pass);
+  void finish_pass (opt_pass *pass);
+
+ private:
+  json::array *m_json;
+  json::object *m_cur_pass_obj;
+  opt_pass *m_cur_pass;
+};
+
+/* FIXME.  */
+
+static fancy_dump *the_fancy_dump;
+
+/* FIXME.  */
+
+fancy_dump::fancy_dump ()
+: m_json (new json::array ()),
+  m_cur_pass_obj (NULL),
+  m_cur_pass (NULL)
+{
+}
+
+/* FIXME.  */
+
+fancy_dump::~fancy_dump ()
+{
+  m_json->dump (stderr);
+  delete m_json;
+}
+
+/* FIXME.  */
+
+void
+fancy_dump::start_pass (opt_pass *pass)
+{
+  gcc_assert (pass);
+  gcc_assert (m_cur_pass == NULL);
+  m_cur_pass = pass;
+  m_cur_pass_obj = new json::object ();
+  m_json->append (m_cur_pass_obj);
+  m_cur_pass_obj->set ("pass", new json::string (pass->name));
+  if (current_function_decl)
+    {
+      const char *fnname = get_fnname_from_decl (current_function_decl);
+      m_cur_pass_obj->set ("function", new json::string (fnname));
+    }
+}
+
+/* FIXME.  */
+
+void
+fancy_dump::finish_pass (opt_pass *pass)
+{
+  gcc_assert (pass);
+  gcc_assert (m_cur_pass == pass);
+  gcc_assert (m_cur_pass_obj);
+
+  if (current_function_decl)
+    m_cur_pass_obj->set ("state_after", new json::string ("FIXME"));
+
+  m_cur_pass = NULL;
+  m_cur_pass_obj = NULL;
+}
+
+/* FIXME.  */
+
+void
+fancy_dump_start ()
+{
+  the_fancy_dump = new fancy_dump ();
+}
+
+/* FIXME.  */
+
+void
+fancy_dump_finish ()
+{
+  delete the_fancy_dump;
+}
+
+/* FIXME.  */
+
+void
+fancy_dump_start_pass (opt_pass *pass)
+{
+  gcc_assert (the_fancy_dump);
+  the_fancy_dump->start_pass (pass);
+}
+
+/* FIXME.  */
+
+void
+fancy_dump_finish_pass (opt_pass *pass)
+{
+  gcc_assert (the_fancy_dump);
+  the_fancy_dump->finish_pass (pass);
+}
diff --git a/gcc/dumpfile.h b/gcc/dumpfile.h
index 21803a6..cb4e66a 100644
--- a/gcc/dumpfile.h
+++ b/gcc/dumpfile.h
@@ -199,6 +199,13 @@ dump_enabled_p (void)
   return (dump_file || alt_dump_file);
 }
 
+extern void fancy_dump_start ();
+extern void fancy_dump_finish ();
+
+struct opt_pass;
+extern void fancy_dump_start_pass (opt_pass *pass);
+extern void fancy_dump_finish_pass (opt_pass *pass);
+
 namespace gcc {
 
 class dump_manager
diff --git a/gcc/passes.c b/gcc/passes.c
index ad0a912..801446b 100644
--- a/gcc/passes.c
+++ b/gcc/passes.c
@@ -2122,6 +2122,8 @@ release_dump_file_name (void)
 bool
 pass_init_dump_file (opt_pass *pass)
 {
+  fancy_dump_start_pass (pass);
+
   /* If a dump file name is present, open it if enabled.  */
   if (pass->static_pass_number != -1)
     {
@@ -2156,6 +2158,8 @@ pass_init_dump_file (opt_pass *pass)
 void
 pass_fini_dump_file (opt_pass *pass)
 {
+  fancy_dump_finish_pass (pass);
+
   timevar_push (TV_DUMP);
 
   /* Flush and close dump file.  */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index b066bcc..96b2a7c 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -488,6 +488,8 @@ compile_file (void)
   if (lang_hooks.decls.post_compilation_parsing_cleanups)
     lang_hooks.decls.post_compilation_parsing_cleanups ();
 
+  fancy_dump_finish ();
+
   if (seen_error ())
     return;
 
@@ -2092,6 +2094,8 @@ do_compile ()
     {
       int i;
 
+      fancy_dump_start ();
+
       timevar_start (TV_PHASE_SETUP);
 
       /* This must be run always, because it is needed to compute the FP
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..6b28cf4
--- /dev/null
+++ b/test.c
@@ -0,0 +1,17 @@
+int foo (int i, int j)
+{
+  int tmp = i * j;
+  return tmp * 3;
+}
+
+int bar (int i, int j)
+{
+  int tmp = i * j;
+  return tmp / 2;
+}
+
+int baz (int i, int j, int k)
+{
+  int tmp = i * j * k;
+  return tmp;
+}
-- 
1.8.5.3

