From 2967b15e0e2b166177f65edcb786e7a725154fbc Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 25 Jun 2018 18:12:33 -0400
Subject: [PATCH 08/44] {committed} dumpfile.c: add indentation via
 DUMP_VECT_SCOPE

gcc/ChangeLog:
	* dumpfile.c (dump_loc): Add indentation based on scope depth.
	(dump_scope_depth): New variable.
	(get_dump_scope_depth): New function.
	(dump_begin_scope): New function.
	(dump_end_scope): New function.
	* dumpfile.h (get_dump_scope_depth): New declaration.
	(dump_begin_scope): New declaration.
	(dump_end_scope): New declaration.
	(class auto_dump_scope): New class.
	(AUTO_DUMP_SCOPE): New macro.
	* tree-vectorizer.h (DUMP_VECT_SCOPE): Reimplement in terms of
	AUTO_DUMP_SCOPE.
---
 gcc/dumpfile.c        | 35 +++++++++++++++++++++++++++++++++++
 gcc/dumpfile.h        | 39 +++++++++++++++++++++++++++++++++++++++
 gcc/tree-vectorizer.h | 15 ++++++++-------
 3 files changed, 82 insertions(+), 7 deletions(-)

diff --git a/gcc/dumpfile.c b/gcc/dumpfile.c
index 06a6673..93bc651 100644
--- a/gcc/dumpfile.c
+++ b/gcc/dumpfile.c
@@ -453,6 +453,8 @@ dump_loc (dump_flags_t dump_kind, FILE *dfile, source_location loc)
                  DECL_SOURCE_FILE (current_function_decl),
                  DECL_SOURCE_LINE (current_function_decl),
                  DECL_SOURCE_COLUMN (current_function_decl));
+      /* Indentation based on scope depth.  */
+      fprintf (dfile, "%*s", get_dump_scope_depth (), "");
     }
 }
 
@@ -573,6 +575,39 @@ template void dump_dec (dump_flags_t, const poly_uint64 &);
 template void dump_dec (dump_flags_t, const poly_offset_int &);
 template void dump_dec (dump_flags_t, const poly_widest_int &);
 
+/* The current dump scope-nesting depth.  */
+
+static int dump_scope_depth;
+
+/* Get the current dump scope-nesting depth.
+   For use by dump_*_loc (for showing nesting via indentation).  */
+
+unsigned int
+get_dump_scope_depth ()
+{
+  return dump_scope_depth;
+}
+
+/* Push a nested dump scope.
+   Print "=== NAME ===\n" to the dumpfile, if any, and to the -fopt-info
+   destination, if any.
+   Increment the scope depth.  */
+
+void
+dump_begin_scope (const char *name, const dump_location_t &loc)
+{
+  dump_printf_loc (MSG_NOTE, loc, "=== %s ===\n", name);
+  dump_scope_depth++;
+}
+
+/* Pop a nested dump scope.  */
+
+void
+dump_end_scope ()
+{
+  dump_scope_depth--;
+}
+
 /* Start a dump for PHASE. Store user-supplied dump flags in
    *FLAG_PTR.  Return the number of streams opened.  Set globals
    DUMP_FILE, and ALT_DUMP_FILE to point to the opened streams, and
diff --git a/gcc/dumpfile.h b/gcc/dumpfile.h
index 489f92e..9828a3f 100644
--- a/gcc/dumpfile.h
+++ b/gcc/dumpfile.h
@@ -459,6 +459,45 @@ dump_enabled_p (void)
   return dumps_are_enabled;
 }
 
+/* Managing nested scopes, so that dumps can express the call chain
+   leading to a dump message.  */
+
+extern unsigned int get_dump_scope_depth ();
+extern void dump_begin_scope (const char *name, const dump_location_t &loc);
+extern void dump_end_scope ();
+
+/* Implementation detail of the AUTO_DUMP_SCOPE macro below.
+
+   A RAII-style class intended to make it easy to emit dump
+   information about entering and exiting a collection of nested
+   function calls.  */
+
+class auto_dump_scope
+{
+ public:
+  auto_dump_scope (const char *name, dump_location_t loc)
+  {
+    if (dump_enabled_p ())
+      dump_begin_scope (name, loc);
+  }
+  ~auto_dump_scope ()
+  {
+    if (dump_enabled_p ())
+      dump_end_scope ();
+  }
+};
+
+/* A macro for calling:
+     dump_begin_scope (NAME, LOC);
+   via an RAII object, thus printing "=== MSG ===\n" to the dumpfile etc,
+   and then calling
+     dump_end_scope ();
+   once the object goes out of scope, thus capturing the nesting of
+   the scopes.  */
+
+#define AUTO_DUMP_SCOPE(NAME, LOC) \
+  auto_dump_scope scope (NAME, LOC)
+
 namespace gcc {
 
 class dump_manager
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 94a0f38..a8406b3 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -1440,15 +1440,16 @@ vect_get_scalar_dr_size (struct data_reference *dr)
 /* Source location + hotness information. */
 extern dump_user_location_t vect_location;
 
-/* If dumping is enabled, emit a MSG_NOTE at vect_location about
-   entering MSG within the vectorizer.  MSG should be a string literal. */
+/* A macro for calling:
+     dump_begin_scope (MSG, vect_location);
+   via an RAII object, thus printing "=== MSG ===\n" to the dumpfile etc,
+   and then calling
+     dump_end_scope ();
+   once the object goes out of scope, thus capturing the nesting of
+   the scopes.  */
 
 #define DUMP_VECT_SCOPE(MSG) \
-  do {						\
-    if (dump_enabled_p ())			\
-      dump_printf_loc (MSG_NOTE, vect_location, \
-		       "=== " MSG " ===\n");	\
-  } while (0)
+  AUTO_DUMP_SCOPE (MSG, vect_location)
 
 /*-----------------------------------------------------------------*/
 /* Function prototypes.                                            */
-- 
1.8.5.3

