From 054bf3d08411d2c6423767461f1dcc3d6032bb9e Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 15 May 2018 11:36:05 -0400
Subject: [PATCH 43/67] Add indentation to show nesting of remarks; split out
 optinfo from optimization records and rename accordingly

---
 gcc/optimization-records.cc |  6 +++---
 gcc/optimization-records.h  |  6 +++---
 gcc/optinfo.cc              | 28 +++++++++++++++++++++++++++-
 gcc/optinfo.h               | 14 +++++++++++---
 gcc/tree-vect-loop.c        |  2 +-
 5 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
index 90c2901..3505a4c 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -155,7 +155,7 @@ opt_record_kind_to_string (enum opt_record_kind kind)
 /* FIXME.  */
 
 bool
-optimization_record_enabled_p ()
+optimization_records_enabled_p ()
 {
   return records != NULL;
 }
@@ -163,7 +163,7 @@ optimization_record_enabled_p ()
 /* FIXME.  */
 
 void
-maybe_record_optinfo (const optinfo *optinfo)
+optimization_records_maybe_record_optinfo (const optinfo *optinfo)
 {
   /* Bail immediately if recording not enabled.  */
   if (!records)
@@ -295,7 +295,7 @@ maybe_record_optinfo (const optinfo *optinfo)
 /* FIXME.  */
 
 void
-maybe_pop_optinfo_scope ()
+optimization_records_maybe_pop_optinfo_scope ()
 {
   /* Bail immediately if recording not enabled.  */
   if (!records)
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
index 1023efd..6477501 100644
--- a/gcc/optimization-records.h
+++ b/gcc/optimization-records.h
@@ -40,10 +40,10 @@ enum opt_record_kind
 extern const char *opt_record_kind_to_string (enum opt_record_kind kind);
 
 // FIXME: maybe make this inline?
-extern bool optimization_record_enabled_p ();
+extern bool optimization_records_enabled_p ();
 
-extern void maybe_record_optinfo (const optinfo *);
+extern void optimization_records_maybe_record_optinfo (const optinfo *);
+extern void optimization_records_maybe_pop_optinfo_scope ();
 
-extern void maybe_pop_optinfo_scope ();
 
 #endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc
index fb8a42a..bb4e999 100644
--- a/gcc/optinfo.cc
+++ b/gcc/optinfo.cc
@@ -107,7 +107,7 @@ void
 optinfo::emit () const
 {
   /* -fsave-optimization-record.  */
-  maybe_record_optinfo (this);
+  optimization_records_maybe_record_optinfo (this);
   emit_optinfo_as_diagnostic_remark (this);
   emit_via_fopt_info ();
 }
@@ -123,6 +123,11 @@ emit_optinfo_as_diagnostic_remark (const optinfo *optinfo)
 
   bool show_color = pp_show_color (global_dc->printer);
 
+  /* Start with scope-based indentation.  */
+  for (unsigned i = get_optinfo_scope_depth (); i > 0; i--)
+    pp_space (&pp);
+
+  /* Print the items into PP.  */
   for (unsigned i = 0; i < optinfo->num_items (); i++)
     {
       const optinfo_item *item = optinfo->get_item (i);
@@ -280,3 +285,24 @@ pending_optinfo::operator<< (const optinfo_printf &p)
   m_optinfo->m_items.safe_push (item);
   return *this;
 }
+
+static unsigned int scope_depth;
+
+void
+push_optinfo_scope ()
+{
+  scope_depth++;
+}
+
+void
+pop_optinfo_scope ()
+{
+  scope_depth--;
+  optimization_records_maybe_pop_optinfo_scope ();
+}
+
+unsigned int
+get_optinfo_scope_depth ()
+{
+  return scope_depth;
+}
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index 2a1ff27..d4d60e5 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -300,6 +300,10 @@ class pending_optinfo
 #define OPTINFO_REMARK(LOC)\
   OPTINFO(OPT_RECORD_REMARK, (LOC))
 
+extern void push_optinfo_scope ();
+extern void pop_optinfo_scope ();
+extern unsigned int get_optinfo_scope_depth ();
+
 /* FIXME.  */
 // RAII-style class intended to make
 // it easy to emit optinfo remarks about entering and exiting the body of a
@@ -312,12 +316,16 @@ class optinfo_scope
   : m_name (name), m_loc (loc)
   {
     if (optinfo_enabled_p ())
-      OPTINFO (OPT_RECORD_SCOPE, loc)
-	<< optinfo_printf ("=== %s ===", name);
+      {
+	OPTINFO (OPT_RECORD_SCOPE, loc)
+	  << optinfo_printf ("=== %s ===", name);
+	push_optinfo_scope ();
+      }
   }
   ~optinfo_scope ()
   {
-    maybe_pop_optinfo_scope ();
+    if (optinfo_enabled_p ())
+      pop_optinfo_scope ();
   }
 
  private:
diff --git a/gcc/tree-vect-loop.c b/gcc/tree-vect-loop.c
index 1f8576f..9e2fe9c 100644
--- a/gcc/tree-vect-loop.c
+++ b/gcc/tree-vect-loop.c
@@ -159,7 +159,7 @@ static void vect_estimate_min_profitable_iters (loop_vec_info, int *, int *);
 
 bool optinfo_enabled_p ()
 {
-  return dump_enabled_p () || optimization_record_enabled_p ();
+  return dump_enabled_p () || optimization_records_enabled_p ();
 }
 
 /* FIXME.  */
-- 
1.8.5.3

