From b1c4b4e507d038da53237256787677295591bece Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 14 May 2018 16:51:05 -0400
Subject: [PATCH 42/83] FIXME: start adding optinfo_scope

---
 gcc/optimization-records.cc | 32 +++++++++++++++++++++++++++++++-
 gcc/optimization-records.h  |  5 ++++-
 gcc/optinfo.cc              |  1 +
 gcc/optinfo.h               | 29 +++++++++++++++++++++++++++++
 gcc/tree-vect-data-refs.c   |  4 +---
 gcc/tree-vectorizer.h       |  3 +++
 6 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
index 4cf4c02..90c2901 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -91,6 +91,10 @@ static json::array *records;
 
 /* FIXME.  */
 
+static vec<json::array *> scopes;
+
+/* FIXME.  */
+
 void
 optimization_records_start ()
 {
@@ -99,6 +103,8 @@ optimization_records_start ()
     return;
 
   records = new json::array ();
+
+  scopes.safe_push (records);
 }
 
 /* FIXME.  */
@@ -141,6 +147,8 @@ opt_record_kind_to_string (enum opt_record_kind kind)
       return "failure";
     case OPT_RECORD_REMARK:
       return "remark";
+    case OPT_RECORD_SCOPE:
+      return "scope";
     }
 }
 
@@ -271,5 +279,27 @@ maybe_record_optinfo (const optinfo *optinfo)
       obj->set ("function", new json::string (fnname));
     }
 
-  records->append (obj);
+  /* Add to innermost scope.  */
+  gcc_assert (scopes.length () > 0);
+  scopes[scopes.length () - 1]->append (obj);
+
+  /* Potentially push the scope.  */
+  if (optinfo->get_kind () == OPT_RECORD_SCOPE)
+    {
+      json::array *children = new json::array ();
+      obj->set ("children", children);
+      scopes.safe_push (children);
+    }
+}
+
+/* FIXME.  */
+
+void
+maybe_pop_optinfo_scope ()
+{
+  /* Bail immediately if recording not enabled.  */
+  if (!records)
+    return;
+
+  scopes.pop ();
 }
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
index 90fa289..1023efd 100644
--- a/gcc/optimization-records.h
+++ b/gcc/optimization-records.h
@@ -33,7 +33,8 @@ enum opt_record_kind
 {
   OPT_RECORD_SUCCESS,
   OPT_RECORD_FAILURE,
-  OPT_RECORD_REMARK
+  OPT_RECORD_REMARK,
+  OPT_RECORD_SCOPE
 };
 
 extern const char *opt_record_kind_to_string (enum opt_record_kind kind);
@@ -43,4 +44,6 @@ extern bool optimization_record_enabled_p ();
 
 extern void maybe_record_optinfo (const optinfo *);
 
+extern void maybe_pop_optinfo_scope ();
+
 #endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc
index d19207f..fb8a42a 100644
--- a/gcc/optinfo.cc
+++ b/gcc/optinfo.cc
@@ -192,6 +192,7 @@ optinfo::emit_via_fopt_info () const
       flags = MSG_MISSED_OPTIMIZATION;
       break;
     case OPT_RECORD_REMARK:
+    case OPT_RECORD_SCOPE:
       flags = MSG_NOTE;
       break;
     }
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index f343be4..2a1ff27 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -301,6 +301,35 @@ class pending_optinfo
   OPTINFO(OPT_RECORD_REMARK, (LOC))
 
 /* FIXME.  */
+// RAII-style class intended to make
+// it easy to emit optinfo remarks about entering and exiting the body of a
+// given function
+
+class optinfo_scope
+{
+ public:
+  optinfo_scope (const char *name, optinfo_location loc)
+  : m_name (name), m_loc (loc)
+  {
+    if (optinfo_enabled_p ())
+      OPTINFO (OPT_RECORD_SCOPE, loc)
+	<< optinfo_printf ("=== %s ===", name);
+  }
+  ~optinfo_scope ()
+  {
+    maybe_pop_optinfo_scope ();
+  }
+
+ private:
+  const char *m_name;
+  optinfo_location m_loc;
+};
+
+/* FIXME.  */
+#define OPTINFO_SCOPE(NAME, LOC) \
+  optinfo_scope scope (NAME, LOC)
+
+/* FIXME.  */
 
 extern void emit_optinfo_as_diagnostic_remark (const optinfo *);
 
diff --git a/gcc/tree-vect-data-refs.c b/gcc/tree-vect-data-refs.c
index d5a3c5a..c55d30a 100644
--- a/gcc/tree-vect-data-refs.c
+++ b/gcc/tree-vect-data-refs.c
@@ -3932,9 +3932,7 @@ vect_analyze_data_refs (vec_info *vinfo, poly_uint64 *min_vf)
   struct data_reference *dr;
   tree scalar_type;
 
-  if (optinfo_enabled_p ())
-    EMIT_OPTINFO_AT_VECT_LOCATION (OPT_RECORD_REMARK) << optinfo_printf (
-		     "=== vect_analyze_data_refs ===");
+  VECT_OPTINFO_SCOPE ("vect_analyze_data_refs");
 
   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
     loop = LOOP_VINFO_LOOP (loop_vinfo);
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 8d8c5a6..5411b37 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -1396,6 +1396,9 @@ emit_optinfo_at_vect_location (const location &impl_location,
 #define EMIT_OPTINFO_AT_VECT_LOCATION(KIND) \
   (emit_optinfo_at_vect_location (OPTINFO_IMPL_LOCATION, (KIND)))
 
+#define VECT_OPTINFO_SCOPE(NAME) \
+  OPTINFO_SCOPE ((NAME), vect_location_thing)
+
 
 /*-----------------------------------------------------------------*/
 /* Function prototypes.                                            */
-- 
1.8.5.3

