From 3841732ee9dabbdf10a70586b54915443931c673 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 14 May 2018 16:01:07 -0400
Subject: [PATCH 38/83] Move the three optinfo emission destinations into
 clearly-separated functions

---
 gcc/optimization-records.cc |  57 ----------------
 gcc/optinfo.cc              | 158 ++++++++++++++++++++++++++++++++------------
 gcc/optinfo.h               |   8 ++-
 3 files changed, 124 insertions(+), 99 deletions(-)

diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
index 8398141..21efe1b 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -272,63 +272,6 @@ maybe_record_optinfo (const optinfo *optinfo)
     }
 
   records->append (obj);
-
-  // FIXME:
-#if 1
-  // Also send it through the diagnostics subsystem:
-  if (1)
-    {
-      pretty_printer pp;
-      pp_needs_newline (&pp) = true;
-      pp_translate_identifiers (&pp) = false;
-
-      bool show_color = true; // FIXME
-
-      for (unsigned i = 0; i < optinfo->num_items (); i++)
-	{
-	  const optinfo_item *item = optinfo->get_item (i);
-	  switch (item->get_kind ())
-	    {
-	    default:
-	      gcc_unreachable ();
-	    case OPTINFO_ITEM_KIND_TEXT:
-	      {
-		const optinfo_item_text *as_text
-		  = (const optinfo_item_text *)item;
-		pp_string (&pp, as_text->get_text ());
-	      }
-	      break;
-	    case OPTINFO_ITEM_KIND_TREE:
-	      {
-		const optinfo_item_tree *as_tree
-		  = (const optinfo_item_tree *)item;
-		pp_begin_quote (&pp, show_color);
-		dump_generic_node (&pp, as_tree->get_node (), 0, TDF_DETAILS,
-				   false);
-		pp_end_quote (&pp, show_color);
-	      }
-	      break;
-	    case OPTINFO_ITEM_KIND_GIMPLE:
-	      {
-		const optinfo_item_gimple *as_gimple
-		  = (const optinfo_item_gimple *)item;
-		gimple *stmt = as_gimple->get_stmt ();
-		pp_begin_quote (&pp, show_color);
-		pp_gimple_stmt_1 (&pp, stmt, 0, TDF_SLIM);
-		pp_end_quote (&pp, show_color);
-	      }
-	      break;
-	    }
-	}
-
-      const char *msg = pp_formatted_text (&pp);
-
-      if (current_pass)
-	remark (loc, 0, "%s [pass=%qs]", msg, current_pass->name);
-      else
-	remark (loc, 0, "%s", msg);
-    }
-#endif
 }
 
 /* FIXME.  */
diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc
index 9b3c5d1..a288902 100644
--- a/gcc/optinfo.cc
+++ b/gcc/optinfo.cc
@@ -46,6 +46,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "optimization-records.h"
 #include "json.h"
 #include "tree-scalar-evolution.h"
+#include "pretty-print.h"
+#include "tree-pretty-print.h"
+#include "gimple-pretty-print.h"
 
 
 /* FIXME.  */
@@ -88,64 +91,137 @@ optinfo::~optinfo ()
 
 /* FIXME.  */
 
+location_t
+optinfo::get_location_t () const
+{
+  if (m_loc.m_stmt)
+    return gimple_location (m_loc.m_stmt);
+  else
+    return UNKNOWN_LOCATION;
+}
+
+/* FIXME.  */
+
 void
 optinfo::emit () const
 {
-  /* -fsave-optimization-record and remarks.  */
+  /* -fsave-optimization-record.  */
   maybe_record_optinfo (this);
+  emit_optinfo_as_diagnostic_remark (this);
+  emit_via_fopt_info ();
+}
+
+/* FIXME.  */
 
-  /* -fopt-info.  */
-  if (1)
+void
+emit_optinfo_as_diagnostic_remark (const optinfo *optinfo)
+{
+  pretty_printer pp;
+  pp_needs_newline (&pp) = true;
+  pp_translate_identifiers (&pp) = false;
+
+  bool show_color = true; // FIXME
+
+  for (unsigned i = 0; i < optinfo->num_items (); i++)
     {
-      dump_flags_t flags = 0;
-      switch (m_kind)
+      const optinfo_item *item = optinfo->get_item (i);
+      switch (item->get_kind ())
 	{
 	default:
 	  gcc_unreachable ();
-	case OPT_RECORD_SUCCESS:
-	  flags = MSG_OPTIMIZED_LOCATIONS;
+	case OPTINFO_ITEM_KIND_TEXT:
+	  {
+	    const optinfo_item_text *as_text
+	      = (const optinfo_item_text *)item;
+	    pp_string (&pp, as_text->get_text ());
+	  }
 	  break;
-	case OPT_RECORD_FAILURE:
-	  flags = MSG_MISSED_OPTIMIZATION;
+	case OPTINFO_ITEM_KIND_TREE:
+	  {
+	    const optinfo_item_tree *as_tree
+	      = (const optinfo_item_tree *)item;
+	    pp_begin_quote (&pp, show_color);
+	    dump_generic_node (&pp, as_tree->get_node (), 0, TDF_DETAILS,
+			       false);
+	    pp_end_quote (&pp, show_color);
+	  }
 	  break;
-	case OPT_RECORD_REMARK:
-	  flags = MSG_NOTE;
+	case OPTINFO_ITEM_KIND_GIMPLE:
+	  {
+	    const optinfo_item_gimple *as_gimple
+	      = (const optinfo_item_gimple *)item;
+	    gimple *stmt = as_gimple->get_stmt ();
+	    pp_begin_quote (&pp, show_color);
+	    pp_gimple_stmt_1 (&pp, stmt, 0, TDF_SLIM);
+	    pp_end_quote (&pp, show_color);
+	  }
 	  break;
 	}
+    }
+
+  const char *msg = pp_formatted_text (&pp);
+
+  location_t loc = optinfo->get_location_t ();
+  if (current_pass)
+    remark (loc, 0, "%s [pass=%qs]", msg, current_pass->name);
+  else
+    remark (loc, 0, "%s", msg);
+}
 
-      // FIXME: location!
-      dump_printf_loc (MSG_NOTE, UNKNOWN_LOCATION, "");
-      for (unsigned i = 0; i < num_items (); i++)
+/* FIXME.  */
+/* -fopt-info.  */
+
+void
+optinfo::emit_via_fopt_info () const
+{
+  dump_flags_t flags = 0;
+  switch (m_kind)
+    {
+    default:
+      gcc_unreachable ();
+    case OPT_RECORD_SUCCESS:
+      flags = MSG_OPTIMIZED_LOCATIONS;
+      break;
+    case OPT_RECORD_FAILURE:
+      flags = MSG_MISSED_OPTIMIZATION;
+      break;
+    case OPT_RECORD_REMARK:
+      flags = MSG_NOTE;
+      break;
+    }
+
+  // FIXME: location!
+  dump_printf_loc (MSG_NOTE, UNKNOWN_LOCATION, "");
+  for (unsigned i = 0; i < num_items (); i++)
+    {
+      const optinfo_item *item = get_item (i);
+      switch (item->get_kind ())
 	{
-	  const optinfo_item *item = get_item (i);
-	  switch (item->get_kind ())
-	    {
-	    default:
-	      gcc_unreachable ();
-	    case OPTINFO_ITEM_KIND_TEXT:
-	      {
-		const optinfo_item_text *text = (const optinfo_item_text *)item;
-		dump_printf (flags, "%s", text->get_text ());
-	      }
-	      break;
-	    case OPTINFO_ITEM_KIND_TREE:
-	      {
-		const optinfo_item_tree *tree = (const optinfo_item_tree *)item;
-		dump_generic_expr (flags, TDF_DETAILS, tree->get_node ());
-	      }
-	      break;
-	    case OPTINFO_ITEM_KIND_GIMPLE:
-	      {
-		const optinfo_item_gimple *as_gimple
-		  = (const optinfo_item_gimple *)item;
-		gimple *stmt = as_gimple->get_stmt ();
-		dump_gimple_stmt (flags, TDF_SLIM, stmt, 0);
-	      }
-	      break;
-	    }
+	default:
+	  gcc_unreachable ();
+	case OPTINFO_ITEM_KIND_TEXT:
+	  {
+	    const optinfo_item_text *text = (const optinfo_item_text *)item;
+	    dump_printf (flags, "%s", text->get_text ());
+	  }
+	  break;
+	case OPTINFO_ITEM_KIND_TREE:
+	  {
+	    const optinfo_item_tree *tree = (const optinfo_item_tree *)item;
+	    dump_generic_expr (flags, TDF_DETAILS, tree->get_node ());
+	  }
+	  break;
+	case OPTINFO_ITEM_KIND_GIMPLE:
+	  {
+	    const optinfo_item_gimple *as_gimple
+	      = (const optinfo_item_gimple *)item;
+	    gimple *stmt = as_gimple->get_stmt ();
+	    dump_gimple_stmt (flags, TDF_SLIM, stmt, 0);
+	  }
+	  break;
 	}
-      dump_printf (flags, "\n");
     }
+  dump_printf (flags, "\n");
 }
 
 /* pending_optinfo's ctor.  */
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index 6d1a218..8745877 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -221,7 +221,11 @@ class optinfo
   unsigned int num_items () const { return m_items.length (); }
   const optinfo_item *get_item (unsigned int i) const { return m_items[i]; }
 
-  void emit () const;
+  location_t get_location_t () const;
+
+ private:
+   void emit () const;
+   void emit_via_fopt_info () const;
 
  private:
   location m_impl_location;
@@ -281,4 +285,6 @@ class pending_optinfo
   std::shared_ptr <optinfo> m_optinfo;
 };
 
+extern void emit_optinfo_as_diagnostic_remark (const optinfo *);
+
 #endif /* #ifndef GCC_OPTINFO_H */
-- 
1.8.5.3

