From 13f8099aeb1b59a168d90c221e0fe1d1c557b65e Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 14 May 2018 16:29:39 -0400
Subject: [PATCH 40/83] Eliminate maybe_record_optimization in favor of
 pending_optinfo throughout

---
 gcc/ipa-inline.c            | 24 ++++++-------
 gcc/optimization-records.cc | 85 ---------------------------------------------
 gcc/optimization-records.h  | 22 ------------
 gcc/optinfo.h               | 17 +++++++++
 gcc/profile.c               |  6 +++-
 gcc/tree-vectorizer.c       |  4 +--
 6 files changed, 35 insertions(+), 123 deletions(-)

diff --git a/gcc/ipa-inline.c b/gcc/ipa-inline.c
index 42b6039..0752ddd 100644
--- a/gcc/ipa-inline.c
+++ b/gcc/ipa-inline.c
@@ -120,7 +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"
+#include "optinfo.h"
 
 typedef fibonacci_heap <sreal, cgraph_edge> edge_heap_t;
 typedef fibonacci_node <sreal, cgraph_edge> edge_heap_node_t;
@@ -2168,12 +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 (OPT_RECORD_SUCCESS, e->call_stmt,
-				   "inlining %s into %s",
-				   xstrdup_for_dump (callee->name ()),
-				   xstrdup_for_dump (e->caller->name ()));
+      if (optinfo_enabled_p ())
+	OPTINFO_SUCCESS (e->call_stmt)
+	  << optinfo_printf ("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)
@@ -2684,12 +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 (OPT_RECORD_SUCCESS, e->call_stmt,
-				   "inlining %s into %s",
-				   xstrdup_for_dump (callee->name ()),
-				   xstrdup_for_dump (e->caller->name ()));
+      if (optinfo_enabled_p ())
+	OPTINFO_SUCCESS (e->call_stmt)
+	  << optinfo_printf ("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 21efe1b..4cf4c02 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -273,88 +273,3 @@ maybe_record_optinfo (const optinfo *optinfo)
 
   records->append (obj);
 }
-
-/* FIXME.  */
-
-void
-maybe_record_optimization_va (enum opt_record_kind kind,
-			      gimple *stmt,
-			      const char *fmt, va_list *ap)
-{
-  /* Bail immediately if recording not enabled.  */
-  if (!records)
-    return;
-
-  /* Formatted output.  */
-  char *msg;
-  vasprintf (&msg, fmt, *ap);
-
-  // FIXME: need to separate the pending from the info
-  {
-    // FIXME: use the actual implementation location
-    pending_optinfo optinfo (OPTINFO_IMPL_LOCATION,
-			     kind,
-			     optinfo_location (stmt));
-    optinfo << msg;
-  }
-
-  free (msg);
-}
-
-/* FIXME.  */
-
-void
-maybe_record_optimization_va (enum opt_record_kind kind,
-			      struct loop *loop,
-			      const char *fmt, va_list *ap)
-{
-  gimple *stmt = get_loop_exit_condition (loop);
-  if (!stmt)
-    {
-      gimple_stmt_iterator si;
-      for (si = gsi_start_bb (loop->header); !gsi_end_p (si); gsi_next (&si))
-	{
-	  if (LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
-	    {
-	      stmt = gsi_stmt (si);
-	      break;
-	    }
-	}
-    }
-
-  maybe_record_optimization_va (kind, stmt, fmt, ap);
-}
-
-/* FIXME.  */
-
-void
-maybe_record_optimization (enum opt_record_kind kind,
-			   gimple *stmt,
-			   const char *fmt, ...)
-{
-  /* Bail immediately if recording not enabled.  */
-  if (!records)
-    return;
-
-  va_list ap;
-  va_start (ap, fmt);
-  maybe_record_optimization_va (kind, stmt, fmt, &ap);
-  va_end (ap);
-}
-
-/* FIXME.  */
-
-void
-maybe_record_optimization (enum opt_record_kind kind,
-			   struct loop *loop,
-			   const char *fmt, ...)
-{
-  /* Bail immediately if recording not enabled.  */
-  if (!records)
-    return;
-
-  va_list ap;
-  va_start (ap, fmt);
-  maybe_record_optimization_va (kind, loop, fmt, &ap);
-  va_end (ap);
-}
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
index a52987b..90fa289 100644
--- a/gcc/optimization-records.h
+++ b/gcc/optimization-records.h
@@ -43,26 +43,4 @@ extern bool optimization_record_enabled_p ();
 
 extern void maybe_record_optinfo (const optinfo *);
 
-extern void maybe_record_optimization_va (enum opt_record_kind kind,
-					  gimple *stmt,
-					  const char *fmt, va_list *ap)
-  ATTRIBUTE_PRINTF (3, 0);
-
-extern void maybe_record_optimization_va (enum opt_record_kind kind,
-					  struct loop *,
-					  const char *fmt, va_list *ap)
-  ATTRIBUTE_PRINTF (3, 0);
-
-// FIXME: param name?  i18n?
-extern void maybe_record_optimization (enum opt_record_kind kind,
-				       gimple *stmt,
-				       const char *fmt, ...)
-  ATTRIBUTE_PRINTF_3;
-
-// FIXME: param name?  i18n?
-extern void maybe_record_optimization (enum opt_record_kind kind,
-				       struct loop *,
-				       const char *fmt, ...)
-  ATTRIBUTE_PRINTF_3;
-
 #endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index 8745877..f343be4 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -285,6 +285,23 @@ class pending_optinfo
   std::shared_ptr <optinfo> m_optinfo;
 };
 
+/* FIXME.  */
+
+#define OPTINFO(KIND, LOC) \
+  pending_optinfo ((OPTINFO_IMPL_LOCATION), (KIND), (LOC))
+
+/* Emit that a successful optimization happened at LOC.  */
+
+#define OPTINFO_SUCCESS(LOC)\
+  OPTINFO(OPT_RECORD_SUCCESS, (LOC))
+
+/* Emit a remark relating to an optimization at LOC.  */
+
+#define OPTINFO_REMARK(LOC)\
+  OPTINFO(OPT_RECORD_REMARK, (LOC))
+
+/* FIXME.  */
+
 extern void emit_optinfo_as_diagnostic_remark (const optinfo *);
 
 #endif /* #ifndef GCC_OPTINFO_H */
diff --git a/gcc/profile.c b/gcc/profile.c
index d1e4699..fc8e5aa 100644
--- a/gcc/profile.c
+++ b/gcc/profile.c
@@ -64,7 +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 "optinfo.h"
 
 #include "profile.h"
 
@@ -1356,9 +1356,13 @@ branch_prob (void)
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
 	{
 	  gimple *stmt = gsi_stmt (gsi);
+	  OPTINFO (OPT_RECORD_REMARK, stmt)
+	    << "test remark";
+#if 0
 	  remark (gimple_location (stmt), 0, "test remark; execution count=%li",
 		  stmt->bb->count.to_gcov_type ());
 	  maybe_record_optimization (OPT_RECORD_REMARK, stmt, "test remark");
+#endif
 #if 0
   /* Expected number of executions: calculated in profile.c.  */
   profile_count count;
diff --git a/gcc/tree-vectorizer.c b/gcc/tree-vectorizer.c
index abab532..492ffff 100644
--- a/gcc/tree-vectorizer.c
+++ b/gcc/tree-vectorizer.c
@@ -663,7 +663,7 @@ vectorize_loops (void)
 	  dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
                        LOCATION_FILE (vect_location),
 		       LOCATION_LINE (vect_location));
-	maybe_record_optimization (OPT_RECORD_REMARK, loop, "analyzing loop");
+	OPTINFO_REMARK (loop) << "analyzing loop";
 
 	loop_vinfo = vect_analyze_loop (loop, orig_loop_vinfo);
 	loop->aux = loop_vinfo;
@@ -741,7 +741,7 @@ vectorize_loops (void)
 	    && dump_enabled_p ())
           dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
                            "loop vectorized\n");
-	maybe_record_optimization (OPT_RECORD_SUCCESS, loop, "loop vectorized");
+	OPTINFO_SUCCESS (loop) << "loop vectorized";
 	new_loop = vect_transform_loop (loop_vinfo);
 	num_vectorized_loops++;
 	/* Now that the loop has been vectorized, allow it to be unrolled
-- 
1.8.5.3

