From 7d1a31d425304bd69473bff88a27792cfef5b560 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 13 Jun 2018 14:08:45 -0400
Subject: [PATCH 29/49] FIXME: further IR dumping (messy)

---
 gcc/optinfo-emit-json.cc | 223 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 222 insertions(+), 1 deletion(-)

diff --git a/gcc/optinfo-emit-json.cc b/gcc/optinfo-emit-json.cc
index caeaf33..db830d0 100644
--- a/gcc/optinfo-emit-json.cc
+++ b/gcc/optinfo-emit-json.cc
@@ -510,6 +510,10 @@ optrecord_json_writer::add_bb_to_json (basic_block bb, json::array *cfg_obj)
       json::object *edge_obj = new json::object ();
       succs->append (edge_obj);
       edge_obj->set ("dest", new json::number (e->dest->index));
+#if 0
+      if (e->probability.initialized_p ())
+	edge_obj->set ("probability", );
+#endif
 
       /* Edge flags.  */
       static const char * const bitnames[] =
@@ -541,22 +545,239 @@ void
 optrecord_json_writer::add_function_state_to_json (function *fun,
 						   json::object *fn_obj)
 {
-  tree old_current_fndecl = current_function_decl;
+  tree arg, var, old_current_fndecl = current_function_decl;
+  struct function *dsf;
+  bool ignore_topmost_bind = false, any_var = false;
   basic_block bb;
+  tree chain;
+#if 0
+  bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
+		  && decl_is_tm_clone (fndecl));
+#endif
+  //struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
   tree fndecl = fun->decl;
 
+#if 0
+  if (DECL_ATTRIBUTES (fndecl) != NULL_TREE)
+    {
+      fprintf (file, "__attribute__((");
+
+      bool first = true;
+      tree chain;
+      for (chain = DECL_ATTRIBUTES (fndecl); chain;
+	   first = false, chain = TREE_CHAIN (chain))
+	{
+	  if (!first)
+	    fprintf (file, ", ");
+
+	  tree name = get_attribute_name (chain);
+	  print_generic_expr (file, name, dump_flags);
+	  if (TREE_VALUE (chain) != NULL_TREE)
+	    {
+	      fprintf (file, " (");
+
+	      if (strstr (IDENTIFIER_POINTER (name), "no_sanitize"))
+		print_no_sanitize_attr_value (file, TREE_VALUE (chain));
+	      else
+		print_generic_expr (file, TREE_VALUE (chain), dump_flags);
+	      fprintf (file, ")");
+	    }
+	}
+
+      fprintf (file, "))\n");
+    }
+#endif
+
   current_function_decl = fndecl;
+#if 0
+  if (flags & TDF_GIMPLE)
+    {
+      print_generic_expr (file, TREE_TYPE (TREE_TYPE (fndecl)),
+			  dump_flags | TDF_SLIM);
+      fprintf (file, " __GIMPLE ()\n%s (", function_name (fun));
+    }
+  else
+    fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
+#endif
+
+  json::array *args = new json::array ();
+  fn_obj->set ("args", args);
+  for (tree arg = DECL_ARGUMENTS (fndecl); arg; arg = DECL_CHAIN (arg))
+    {
+      json::object *arg_obj = new json::object ();
+#if 0      
+      print_generic_expr (file, TREE_TYPE (arg), dump_flags);
+      print_generic_expr (file, arg, dump_flags);
+#endif      
+    }
+
+#if 0
+  dsf = DECL_STRUCT_FUNCTION (fndecl);
+  if (dsf && (flags & TDF_EH))
+    dump_eh_tree (file, dsf);
+#endif
+
+#if 0
+  if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
+    {
+      dump_node (fndecl, TDF_SLIM | flags, file);
+      current_function_decl = old_current_fndecl;
+      return;
+    }
+#endif
+
+#if 0
+  /* When GIMPLE is lowered, the variables are no longer available in
+     BIND_EXPRs, so display them separately.  */
+  if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
+    {
+      unsigned ix;
+      ignore_topmost_bind = true;
+
+      fprintf (file, "{\n");
+      if (gimple_in_ssa_p (fun)
+	  && (flags & TDF_ALIAS))
+	{
+	  for (arg = DECL_ARGUMENTS (fndecl); arg != NULL;
+	       arg = DECL_CHAIN (arg))
+	    {
+	      tree def = ssa_default_def (fun, arg);
+	      if (def)
+		dump_default_def (file, def, 2, flags);
+	    }
+
+	  tree res = DECL_RESULT (fun->decl);
+	  if (res != NULL_TREE
+	      && DECL_BY_REFERENCE (res))
+	    {
+	      tree def = ssa_default_def (fun, res);
+	      if (def)
+		dump_default_def (file, def, 2, flags);
+	    }
+
+	  tree static_chain = fun->static_chain_decl;
+	  if (static_chain != NULL_TREE)
+	    {
+	      tree def = ssa_default_def (fun, static_chain);
+	      if (def)
+		dump_default_def (file, def, 2, flags);
+	    }
+	}
+
+      if (!vec_safe_is_empty (fun->local_decls))
+	FOR_EACH_LOCAL_DECL (fun, ix, var)
+	  {
+	    print_generic_decl (file, var, flags);
+	    fprintf (file, "\n");
+
+	    any_var = true;
+	  }
+
+      tree name;
+
+      if (gimple_in_ssa_p (cfun))
+	FOR_EACH_SSA_NAME (ix, name, cfun)
+	  {
+	    if (!SSA_NAME_VAR (name))
+	      {
+		fprintf (file, "  ");
+		print_generic_expr (file, TREE_TYPE (name), flags);
+		fprintf (file, " ");
+		print_generic_expr (file, name, flags);
+		fprintf (file, ";\n");
+
+		any_var = true;
+	      }
+	  }
+    }
+#endif
 
   if (fun && fun->decl == fndecl
       && fun->cfg
       && basic_block_info_for_fn (fun))
     {
+#if 0
+      /* If the CFG has been built, emit a CFG-based dump.  */
+      if (!ignore_topmost_bind)
+	fprintf (file, "{\n");
+
+      if (any_var && n_basic_blocks_for_fn (fun))
+	fprintf (file, "\n");
+#endif
+
       json::array *cfg_obj = new json::array ();
       fn_obj->set ("cfg", cfg_obj);
 
       FOR_ALL_BB_FN (bb, fun)
 	add_bb_to_json (bb, cfg_obj);
     }
+  else if (fun->curr_properties & PROP_gimple_any)
+    {
+#if 0
+      /* The function is now in GIMPLE form but the CFG has not been
+	 built yet.  Emit the single sequence of GIMPLE statements
+	 that make up its body.  */
+      gimple_seq body = gimple_body (fndecl);
+
+      if (gimple_seq_first_stmt (body)
+	  && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
+	  && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
+	print_gimple_seq (file, body, 0, flags);
+      else
+	{
+	  if (!ignore_topmost_bind)
+	    fprintf (file, "{\n");
+
+	  if (any_var)
+	    fprintf (file, "\n");
+
+	  print_gimple_seq (file, body, 2, flags);
+	  fprintf (file, "}\n");
+	}
+#endif
+    }
+  else
+    {
+#if 0
+      int indent;
+
+      /* Make a tree based dump.  */
+      chain = DECL_SAVED_TREE (fndecl);
+      if (chain && TREE_CODE (chain) == BIND_EXPR)
+	{
+	  if (ignore_topmost_bind)
+	    {
+	      chain = BIND_EXPR_BODY (chain);
+	      indent = 2;
+	    }
+	  else
+	    indent = 0;
+	}
+      else
+	{
+	  if (!ignore_topmost_bind)
+	    {
+	      fprintf (file, "{\n");
+	      /* No topmost bind, pretend it's ignored for later.  */
+	      ignore_topmost_bind = true;
+	    }
+	  indent = 2;
+	}
+
+      if (any_var)
+	fprintf (file, "\n");
+
+      print_generic_stmt_indented (file, chain, flags, indent);
+      if (ignore_topmost_bind)
+	fprintf (file, "}\n");
+#endif
+    }
+
+#if 0
+  if (flags & TDF_ENUMERATE_LOCALS)
+    dump_enumerated_decls (file, flags);
+  fprintf (file, "\n\n");
+#endif
 
   current_function_decl = old_current_fndecl;
 }
-- 
1.8.5.3

