From 1944be10e8d3eb4a21ef8cbaf026063744b94ae0 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 17 Jul 2018 04:13:26 -0400
Subject: [PATCH 55/56] FIXME: WIP: use diagram.h to show BBs

---
 gcc/tree-vectorizer.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 162 insertions(+)

diff --git a/gcc/tree-vectorizer.c b/gcc/tree-vectorizer.c
index c60d0d9..932bfd1 100644
--- a/gcc/tree-vectorizer.c
+++ b/gcc/tree-vectorizer.c
@@ -80,6 +80,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "attribs.h"
 #include "gimple-pretty-print.h"
 #include "opt-problem.h"
+#include "diagram.h"
 
 
 /* Loop or bb location, with hotness information.  */
@@ -685,6 +686,163 @@ set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
   free (bbs);
 }
 
+static element *
+make_element_for_stmt (gimple *stmt)
+{
+  pretty_printer pp;
+  pp_needs_newline (&pp) = true;
+  pp_string (&pp, "  ");
+  pp_gimple_stmt_1 (&pp, stmt, /*spc=*/0, /*dump_flags=*/0);
+
+  return new text_element (pp_formatted_text (&pp));
+}
+
+static element *
+make_element_for_bb (basic_block bb)
+{
+  auto_vec<element *> rows;
+
+  {
+    pretty_printer pp;
+    pp_printf (&pp, "BLOCK %i:", bb->index);
+    rows.safe_push (new text_element (pp_formatted_text (&pp)));
+  }
+
+  location_t loc = UNKNOWN_LOCATION;
+  const char *last_file_path = NULL;
+  int last_linenum = -1;
+
+  for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
+       gsi_next (&si))
+    {
+      gphi *phi = si.phi ();
+      rows.safe_push (make_element_for_stmt (phi));
+    }
+
+  for (gimple_stmt_iterator si = gsi_start_bb (bb);
+       !gsi_end_p (si); gsi_next (&si))
+    {
+      gimple *stmt = gsi_stmt (si);
+      if (loc != stmt->location)
+	{
+	  loc = stmt->location;
+	  if (loc != UNKNOWN_LOCATION)
+	    {
+	      const char *file_path = LOCATION_FILE (loc);
+	      int linenum = LOCATION_LINE (loc);
+	      if (file_path
+		  && (file_path != last_file_path
+		      || linenum != last_linenum))
+		{
+		  last_file_path = file_path;
+		  last_linenum = linenum;
+
+		  char *line
+		    = location_get_source_line (file_path, linenum).xstrdup ();
+		  pretty_printer pp;
+		  pp_printf (&pp, "%s:%i: %s", file_path, linenum, line);
+		  free (line);
+		  rows.safe_push (new text_element (pp_formatted_text (&pp)));
+		}
+	    }
+	}
+      rows.safe_push (make_element_for_stmt (stmt));
+    }
+
+  grid_layout *grid = new grid_layout (1, rows.length ());
+  for (unsigned i = 0; i < rows.length (); i++)
+    grid->set_child (0, i, rows[i]);
+
+  box_element *box = new box_element (grid);
+  return box;
+}
+
+class cfg_element : public wrapper_element
+{
+ public:
+  cfg_element (function *fun) : wrapper_element (NULL)
+  {
+    basic_block bb;
+    FOR_EACH_BB_FN (bb, fun)
+      {
+	if (!block_to_element.get (bb))
+	  {
+	    element *bb_element = add_element_for_bb (bb);
+	    rows.safe_push (bb_element);
+	  }
+      }
+    grid_layout *grid = new grid_layout (1, rows.length ());
+    for (unsigned i = 0; i < rows.length (); i++)
+      grid->set_child (0, i, rows[i]);
+    set_child (grid);
+  }
+
+ private:
+  element *add_element_for_bb (basic_block bb)
+  {
+    if (block_to_element.get (bb))
+      return *block_to_element.get (bb);
+
+    element *bb_element = make_element_for_bb (bb);
+    block_to_element.put (bb, bb_element);
+    auto_vec <basic_block> succs;
+    edge e;
+    edge_iterator ei;
+    FOR_EACH_EDGE (e, ei, bb->succs)
+      succs.safe_push (e->dest);
+    if (succs.length () > 0)
+      {
+	grid_layout *wiring_grid = new grid_layout (succs.length (), 3);
+	unsigned cx = succs.length () / 2;
+	wiring_grid->set_child (cx, 0, bb_element);
+	for (unsigned x = 0; x < succs.length (); x++)
+	  {
+	    wiring_grid->set_child (x, 1,
+				    new wiring_element (x == cx,
+							true,
+							x < succs.length () - 1,
+							x > 0));
+	    if (block_to_element.get (succs[x]))
+	      {
+		/* Already visited this one.  */
+		pretty_printer pp;
+		pp_printf (&pp, "block %i", succs[x]->index);
+		wiring_grid->set_child (x, 2,
+					new box_element (new text_element (pp_formatted_text (&pp))));
+	      }
+	    else
+	      wiring_grid->set_child (x, 2,
+				      add_element_for_bb (succs[x]));
+	  }
+	return wiring_grid;
+      }
+    else
+      return bb_element;
+  }
+
+ private:
+  hash_map <basic_block, element *> block_to_element;
+  auto_vec<element *> rows;
+};
+
+static element *
+make_element_for_cfg (function *fun)
+{
+  return new cfg_element (fun);
+}
+
+static void
+show_diagram (loop_vec_info loop_vinfo)
+{
+  element *el = make_element_for_cfg (cfun);
+
+  pretty_printer pp;
+  el->print_to_pp (&pp);
+  pp_flush (&pp);
+
+  delete el;
+}
+
 /* Try to vectorize LOOP.  */
 
 static unsigned
@@ -795,6 +953,8 @@ try_vectorize_loop_1 (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
 		     "loop vectorized using "
 		     HOST_WIDE_INT_PRINT_UNSIGNED " byte "
 		     "vectors", bytes);
+
+      show_diagram (loop_vinfo);
     }
   else
     {
@@ -811,6 +971,8 @@ try_vectorize_loop_1 (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
      etc.  */
   loop->force_vectorize = false;
 
+  show_diagram (loop_vinfo);
+
   if (loop->simduid)
     {
       simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
-- 
1.8.5.3

