From e27c6686280ff2f59a89e19ef404a42e758b4add Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 23 May 2018 13:00:48 -0400
Subject: [PATCH 13/24] Experiment with using optinfo in
 gimple-loop-interchange.cc

This was an experiment to try to capture information on a
loop optimization.

gcc/ChangeLog:
	* gimple-loop-interchange.cc (should_interchange_loops): Add
	optinfo note when interchange gives better data locality behavior.
	(tree_loop_interchange::interchange): Add OPTINFO_SCOPE.
	Add optinfo for successful and unsuccessful interchanges.
	(prepare_perfect_loop_nest): Add OPTINFO_SCOPE.  Add
	optinfo note.
	(pass_linterchange::execute): Add OPTINFO_SCOPE.
---
 gcc/gimple-loop-interchange.cc | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/gcc/gimple-loop-interchange.cc b/gcc/gimple-loop-interchange.cc
index eb35263..cd32288 100644
--- a/gcc/gimple-loop-interchange.cc
+++ b/gcc/gimple-loop-interchange.cc
@@ -1556,7 +1556,19 @@ should_interchange_loops (unsigned i_idx, unsigned o_idx,
   ratio = innermost_loops_p ? INNER_STRIDE_RATIO : OUTER_STRIDE_RATIO;
   /* Do interchange if it gives better data locality behavior.  */
   if (wi::gtu_p (iloop_strides, wi::mul (oloop_strides, ratio)))
-    return true;
+    {
+      if (optinfo_enabled_p ())
+	OPTINFO_NOTE ((gimple *)NULL) // FIXME
+	  << "interchange gives better data locality behavior: "
+	  << "iloop_strides: "
+	  << decu (iloop_strides)
+	  << " > (oloop_strides: "
+	  << decu (oloop_strides)
+	  << " * ratio: "
+	  << decu (ratio)
+	  << ")";
+      return true;
+    }
   if (wi::gtu_p (iloop_strides, oloop_strides))
     {
       /* Or it creates more invariant memory references.  */
@@ -1578,6 +1590,8 @@ bool
 tree_loop_interchange::interchange (vec<data_reference_p> datarefs,
 				    vec<ddr_p> ddrs)
 {
+  OPTINFO_SCOPE ("tree_loop_interchange::interchange", m_loop_nest[0]);
+
   location_t loc = find_loop_location (m_loop_nest[0]);
   bool changed_p = false;
   /* In each iteration we try to interchange I-th loop with (I+1)-th loop.
@@ -1628,6 +1642,10 @@ tree_loop_interchange::interchange (vec<data_reference_p> datarefs,
 	    fprintf (dump_file,
 		     "Loop_pair<outer:%d, inner:%d> is interchanged\n\n",
 		     oloop.m_loop->num, iloop.m_loop->num);
+	  if (optinfo_enabled_p ())
+	    OPTINFO_SUCCESS (oloop.m_loop)
+	      << optinfo_printf ("Loop_pair<outer:%d, inner:%d> is interchanged",
+				 oloop.m_loop->num, iloop.m_loop->num);
 
 	  changed_p = true;
 	  interchange_loops (iloop, oloop);
@@ -1641,6 +1659,10 @@ tree_loop_interchange::interchange (vec<data_reference_p> datarefs,
 	    fprintf (dump_file,
 		     "Loop_pair<outer:%d, inner:%d> is not interchanged\n\n",
 		     oloop.m_loop->num, iloop.m_loop->num);
+	  if (optinfo_enabled_p ())
+	    OPTINFO_FAILURE (oloop.m_loop)
+	      << optinfo_printf ("Loop_pair<outer:%d, inner:%d> is not interchanged",
+				 oloop.m_loop->num, iloop.m_loop->num);
 	}
     }
   simple_dce_from_worklist (m_dce_seeds);
@@ -1648,6 +1670,9 @@ tree_loop_interchange::interchange (vec<data_reference_p> datarefs,
   if (changed_p)
     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
 		     "loops interchanged in loop nest\n");
+  if (optinfo_enabled_p ())
+    OPTINFO_SUCCESS (m_loop_nest[0])
+      << "loops interchanged in loop nest";
 
   return changed_p;
 }
@@ -1971,6 +1996,8 @@ static bool
 prepare_perfect_loop_nest (struct loop *loop, vec<loop_p> *loop_nest,
 			   vec<data_reference_p> *datarefs, vec<ddr_p> *ddrs)
 {
+  OPTINFO_SCOPE ("prepare_perfect_loop_nest", loop);
+
   struct loop *start_loop = NULL, *innermost = loop;
   struct loop *outermost = loops_for_fn (cfun)->tree_root;
 
@@ -2029,6 +2056,12 @@ prepare_perfect_loop_nest (struct loop *loop, vec<loop_p> *loop_nest,
 	  fprintf (dump_file,
 		   "\nConsider loop interchange for loop_nest<%d - %d>\n",
 		   start_loop->num, innermost->num);
+	if (optinfo_enabled_p ())
+	  {
+	    OPTINFO_NOTE (start_loop)
+	      << optinfo_printf ("consider loop interchange for loop_nest<%d - %d>",
+				 start_loop->num, innermost->num);
+	  }
 
 	if (loop != start_loop)
 	  prune_access_strides_not_in_loop (start_loop, innermost, *datarefs);
@@ -2061,6 +2094,7 @@ pass_linterchange::execute (function *fun)
   struct loop *loop;
   FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
     {
+      OPTINFO_SCOPE ("considering loop for interchange", loop);
       vec<loop_p> loop_nest = vNULL;
       vec<data_reference_p> datarefs = vNULL;
       vec<ddr_p> ddrs = vNULL;
-- 
1.8.5.3

