From 60e036a652b1ccf1aef17c322eeb1ecd55cf2605 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 16 May 2018 15:58:19 -0400
Subject: [PATCH 66/67] Handle TDF_DETAILS; start using optinfo for loop
 unrolling

---
 000-README.rst                |  5 +++++
 gcc/optinfo-emit-fopt-info.cc |  2 ++
 gcc/optinfo.cc                |  5 +++--
 gcc/optinfo.h                 | 28 +++++++++++++++++++++++-----
 gcc/tree-ssa-loop-ivcanon.c   | 19 +++++++++----------
 gcc/tree-vect-loop.c          |  2 +-
 6 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/000-README.rst b/000-README.rst
index 72a150b..52f96e1 100644
--- a/000-README.rst
+++ b/000-README.rst
@@ -287,6 +287,11 @@ Known unknowns
 * Should optimization info be internationalized/localized? Currently
   `-fopt-info` isn't.  I say let's *not* i18n it.
 
+* My prototype respects TDF_DETAILS for the existing `-fopt-info` output,
+  but given that it appears to have been applied haphazardly, and that we
+  can filter in other ways, my prototype ignores it for the other output
+  formats.
+
 
 Notes to self
 =============
diff --git a/gcc/optinfo-emit-fopt-info.cc b/gcc/optinfo-emit-fopt-info.cc
index 10bfdec..803be27 100644
--- a/gcc/optinfo-emit-fopt-info.cc
+++ b/gcc/optinfo-emit-fopt-info.cc
@@ -53,6 +53,8 @@ emit_optinfo_via_fopt_info (const optinfo *optinfo)
       flags = MSG_NOTE;
       break;
     }
+  if (optinfo->details_p ())
+    flags |= TDF_DETAILS;
 
   // FIXME: location!
   dump_printf_loc (MSG_NOTE, UNKNOWN_LOCATION, "");
diff --git a/gcc/optinfo.cc b/gcc/optinfo.cc
index 1ed68f0..9cc982a 100644
--- a/gcc/optinfo.cc
+++ b/gcc/optinfo.cc
@@ -123,8 +123,9 @@ optinfo::emit () const
 
 pending_optinfo::pending_optinfo (const optinfo_impl_location &impl_location,
 				  enum optinfo_kind kind,
-				  optinfo_location loc)
-  : m_optinfo (new optinfo (impl_location, kind, loc))
+				  optinfo_location loc,
+				  bool details)
+  : m_optinfo (new optinfo (impl_location, kind, loc, details))
 {
 }
 
diff --git a/gcc/optinfo.h b/gcc/optinfo.h
index a9d4f77..8330236 100644
--- a/gcc/optinfo.h
+++ b/gcc/optinfo.h
@@ -158,8 +158,10 @@ class optinfo
  public:
   optinfo (const optinfo_impl_location &impl_location,
 	   enum optinfo_kind kind,
-	   optinfo_location loc)
-    : m_impl_location (impl_location), m_kind (kind), m_loc (loc), m_items ()
+	   optinfo_location loc,
+	   bool details)
+  : m_impl_location (impl_location), m_kind (kind), m_loc (loc), m_items (),
+    m_details (details)
   {}
   ~optinfo ();
 
@@ -168,6 +170,7 @@ class optinfo
   optinfo_location get_location () const { return m_loc; }
   unsigned int num_items () const { return m_items.length (); }
   const optinfo_item *get_item (unsigned int i) const { return m_items[i]; }
+  bool details_p () const { return m_details; }
 
   location_t get_location_t () const;
 
@@ -179,6 +182,8 @@ class optinfo
   enum optinfo_kind m_kind;
   optinfo_location m_loc;
   auto_vec <optinfo_item *> m_items;
+  /* If true, gate this with TDF_DETAILS when emitting to dump/-fopt-info.  */
+  bool m_details;
 };
 
 /* A class for adding printf-style formatted strings to an optinfo.  */
@@ -210,7 +215,8 @@ class pending_optinfo
  public:
   pending_optinfo (const optinfo_impl_location &impl_location,
 		   enum optinfo_kind kind,
-		   optinfo_location loc);
+		   optinfo_location loc,
+		   bool details);
 
   pending_optinfo& operator<< (const char *);
   pending_optinfo& operator<< (optinfo_tree);
@@ -246,7 +252,7 @@ class pending_optinfo
 /* Emit optimization information at LOC of the given KIND.  */
 
 #define OPTINFO(KIND, LOC) \
-  pending_optinfo ((OPTINFO_IMPL_LOCATION), (KIND), (LOC))
+  pending_optinfo ((OPTINFO_IMPL_LOCATION), (KIND), (LOC), false)
 
 /* Emit that a successful optimization happened at LOC.  */
 
@@ -263,6 +269,18 @@ class pending_optinfo
 #define OPTINFO_NOTE(LOC)\
   OPTINFO(OPTINFO_KIND_NOTE, (LOC))
 
+/* Emit optimization information at LOC of the given KIND,
+   tagging it with TDF_DETAILS for -fopt-info.  */
+
+#define OPTINFO_DETAILS(KIND, LOC) \
+  pending_optinfo ((OPTINFO_IMPL_LOCATION), (KIND), (LOC), true)
+
+/* Emit that a successful optimization happened at LOC,
+   tagging it with TDF_DETAILS for -fopt-info.  */
+
+#define OPTINFO_SUCCESS_DETAILS(LOC)\
+  OPTINFO_DETAILS(OPTINFO_KIND_SUCCESS, (LOC))
+
 extern void push_optinfo_scope ();
 extern void pop_optinfo_scope ();
 extern unsigned int get_optinfo_scope_depth ();
@@ -281,7 +299,7 @@ class optinfo_scope
   {
     if (optinfo_enabled_p ())
       {
-	pending_optinfo (impl_location, OPTINFO_KIND_SCOPE, loc)
+	pending_optinfo (impl_location, OPTINFO_KIND_SCOPE, loc, false)
 	  << optinfo_printf ("=== %s ===", name);
 	push_optinfo_scope ();
       }
diff --git a/gcc/tree-ssa-loop-ivcanon.c b/gcc/tree-ssa-loop-ivcanon.c
index 24bf60e..bfd325e 100644
--- a/gcc/tree-ssa-loop-ivcanon.c
+++ b/gcc/tree-ssa-loop-ivcanon.c
@@ -63,6 +63,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-inline.h"
 #include "tree-cfgcleanup.h"
 #include "builtins.h"
+#include "optinfo.h"
 
 /* Specifies types of loops that may be unrolled.  */
 
@@ -936,21 +937,19 @@ try_unroll_loop_completely (struct loop *loop,
   loops_to_unloop.safe_push (loop);
   loops_to_unloop_nunroll.safe_push (n_unroll);
 
-  if (dump_enabled_p ())
+  if (optinfo_enabled_p ())
     {
       if (!n_unroll)
-        dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
-                         "loop turned into non-loop; it never loops\n");
+	OPTINFO_SUCCESS_DETAILS (loop)
+	  << "loop turned into non-loop; it never loops";
       else
         {
-          dump_printf_loc (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, locus,
-                           "loop with %d iterations completely unrolled",
-			   (int) n_unroll);
+	  pending_optinfo info = OPTINFO_SUCCESS_DETAILS (loop)
+	    << optinfo_printf ("loop with %d iterations completely unrolled",
+			       (int) n_unroll);
           if (loop->header->count.initialized_p ())
-            dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS,
-                         " (header execution count %d)",
-                         (int)loop->header->count.to_gcov_type ());
-          dump_printf (MSG_OPTIMIZED_LOCATIONS | TDF_DETAILS, "\n");
+            info << optinfo_printf (" (header execution count %d)",
+				    (int)loop->header->count.to_gcov_type ());
         }
     }
 
diff --git a/gcc/tree-vect-loop.c b/gcc/tree-vect-loop.c
index 6e6da09..eccaf7a 100644
--- a/gcc/tree-vect-loop.c
+++ b/gcc/tree-vect-loop.c
@@ -162,7 +162,7 @@ pending_optinfo
 emit_optinfo_at_vect_location (const optinfo_impl_location &impl_location,
 			       enum optinfo_kind kind)
 {
-  return pending_optinfo (impl_location, kind, vect_optinfo_location);
+  return pending_optinfo (impl_location, kind, vect_optinfo_location, false);
 }
 
 /* Function vect_determine_vectorization_factor
-- 
1.8.5.3

