From 045c0435b19ed7406087c2d224179f335523a203 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 May 2018 20:46:14 -0400
Subject: [PATCH 21/83] FIXME: start recording vectorization

---
 gcc/optimization-records.cc | 63 ++++++++++++++++++++++++++++++++++++++-------
 gcc/optimization-records.h  |  8 ++++++
 gcc/tree-vectorizer.c       |  3 +++
 3 files changed, 65 insertions(+), 9 deletions(-)

diff --git a/gcc/optimization-records.cc b/gcc/optimization-records.cc
index 164d48a..c020f85 100644
--- a/gcc/optimization-records.cc
+++ b/gcc/optimization-records.cc
@@ -44,6 +44,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "optimization-records.h"
 #include "json.h"
+#include "tree-scalar-evolution.h"
 
 /* FIXME.  */
 
@@ -127,13 +128,12 @@ opt_record_kind_to_string (enum opt_record_kind kind)
     }
 }
 
-
 /* FIXME.  */
 
-void
-maybe_record_optimization (enum opt_record_kind kind,
-			   gimple *stmt,
-			   const char *fmt, ...)
+static void
+maybe_record_optimization_va (enum opt_record_kind kind,
+			      gimple *stmt,
+			      const char *fmt, va_list *ap)
 {
   /* Bail immediately if recording not enabled.  */
   if (!records)
@@ -160,16 +160,61 @@ maybe_record_optimization (enum opt_record_kind kind,
     }
 
   /* Formatted output.  */
-  va_list ap;
-  va_start (ap, fmt);
   char *msg;
-  vasprintf (&msg, fmt, ap);
+  vasprintf (&msg, fmt, *ap);
   obj->set ("message", new json::string (msg));
   free (msg);
-  va_end (ap);
 
   if (stmt->bb->count.initialized_p ())
     obj->set ("count", profile_count_to_json (stmt->bb->count));
 
   records->append (obj);
 }
+
+/* FIXME.  */
+
+void
+maybe_record_optimization (enum opt_record_kind kind,
+			   gimple *stmt,
+			   const char *fmt, ...)
+{
+  /* Bail immediately if recording not enabled.  */
+  if (!records)
+    return;
+
+  va_list ap;
+  va_start (ap, fmt);
+  maybe_record_optimization_va (kind, stmt, fmt, &ap);
+  va_end (ap);
+}
+
+/* FIXME.  */
+
+void
+maybe_record_optimization (enum opt_record_kind kind,
+			   struct loop *loop,
+			   const char *fmt, ...)
+{
+  /* Bail immediately if recording not enabled.  */
+  if (!records)
+    return;
+
+  gimple *stmt = get_loop_exit_condition (loop);
+  if (!stmt)
+    {
+      gimple_stmt_iterator si;
+      for (si = gsi_start_bb (loop->header); !gsi_end_p (si); gsi_next (&si))
+	{
+	  if (LOCATION_LOCUS (gimple_location (stmt)) > BUILTINS_LOCATION)
+	    {
+	      stmt = gsi_stmt (si);
+	      break;
+	    }
+	}
+    }
+
+  va_list ap;
+  va_start (ap, fmt);
+  maybe_record_optimization_va (kind, stmt, fmt, &ap);
+  va_end (ap);
+}
diff --git a/gcc/optimization-records.h b/gcc/optimization-records.h
index 3860913..8513781 100644
--- a/gcc/optimization-records.h
+++ b/gcc/optimization-records.h
@@ -21,6 +21,8 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_OPTIMIZATION_RECORDS_H
 #define GCC_OPTIMIZATION_RECORDS_H
 
+struct loop;
+
 /* FIXME.  */
 
 extern void optimization_records_start ();
@@ -41,4 +43,10 @@ extern void maybe_record_optimization (enum opt_record_kind kind,
 				       const char *fmt, ...)
   ATTRIBUTE_PRINTF_3;
 
+// FIXME: param name?  i18n?
+extern void maybe_record_optimization (enum opt_record_kind kind,
+				       struct loop *,
+				       const char *fmt, ...)
+  ATTRIBUTE_PRINTF_3;
+
 #endif /* #ifndef GCC_OPTIMIZATION_RECORDS_H */
diff --git a/gcc/tree-vectorizer.c b/gcc/tree-vectorizer.c
index fb81b98..8f18fcd 100644
--- a/gcc/tree-vectorizer.c
+++ b/gcc/tree-vectorizer.c
@@ -78,6 +78,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-scalar-evolution.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "optimization-records.h"
 
 
 /* Loop or bb location.  */
@@ -660,6 +661,7 @@ vectorize_loops (void)
 	  dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
                        LOCATION_FILE (vect_location),
 		       LOCATION_LINE (vect_location));
+	maybe_record_optimization (OPT_RECORD_REMARK, loop, "analyzing loop");
 
 	loop_vinfo = vect_analyze_loop (loop, orig_loop_vinfo);
 	loop->aux = loop_vinfo;
@@ -737,6 +739,7 @@ vectorize_loops (void)
 	    && dump_enabled_p ())
           dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
                            "loop vectorized\n");
+	maybe_record_optimization (OPT_RECORD_SUCCESS, loop, "loop vectorized");
 	new_loop = vect_transform_loop (loop_vinfo);
 	num_vectorized_loops++;
 	/* Now that the loop has been vectorized, allow it to be unrolled
-- 
1.8.5.3

