From f69c9ec302098240e3fdf9a79f0a4d93b8329ab5 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 20 Mar 2019 14:49:55 -0400
Subject: [PATCH 128/161] FIXME: non-working start of exploded graph experiment

---
 gcc/analyzer/Make-lang.in              |   2 +
 gcc/analyzer/digraph.cc                | 102 +++++++++++++++++++++++++++++++++
 gcc/analyzer/digraph.h                 |  59 +++++++++++++++++++
 gcc/analyzer/engine.cc                 |  15 +++--
 gcc/analyzer/exploded-graph.cc         |  45 +++++++++++++++
 gcc/analyzer/exploded-graph.h          |  59 +++++++++++++++++++
 gcc/analyzer/hardcoded-state-machine.h |  25 ++++++++
 7 files changed, 302 insertions(+), 5 deletions(-)
 create mode 100644 gcc/analyzer/digraph.cc
 create mode 100644 gcc/analyzer/digraph.h
 create mode 100644 gcc/analyzer/exploded-graph.cc
 create mode 100644 gcc/analyzer/exploded-graph.h

diff --git a/gcc/analyzer/Make-lang.in b/gcc/analyzer/Make-lang.in
index a838bb3..b7c738d 100644
--- a/gcc/analyzer/Make-lang.in
+++ b/gcc/analyzer/Make-lang.in
@@ -58,7 +58,9 @@ ANALYZER_PLUGIN_SO_OBJS = \
 	analyzer/analyzer-plugin.o \
 	analyzer/checker-script.o \
 	analyzer/constraints.o \
+	analyzer/digraph.o \
 	analyzer/engine.o \
+	analyzer/exploded-graph.o \
 	analyzer/graphviz.o \
 	analyzer/hardcoded-state-machine.o \
 	analyzer/hardcoded-state-machine-malloc.o \
diff --git a/gcc/analyzer/digraph.cc b/gcc/analyzer/digraph.cc
new file mode 100644
index 0000000..09218f3
--- /dev/null
+++ b/gcc/analyzer/digraph.cc
@@ -0,0 +1,102 @@
+/* FIXME.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "gcc-plugin.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tm.h"
+#include "toplev.h"
+#include "hash-table.h"
+#include "vec.h"
+#include "ggc.h"
+#include "basic-block.h"
+#include "tree-ssa-alias.h"
+#include "internal-fn.h"
+#include "gimple-fold.h"
+#include "tree-eh.h"
+#include "gimple-expr.h"
+#include "is-a.h"
+#include "gimple.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "context.h"
+#include "diagnostic.h"
+#include "bitmap.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "gimple-pretty-print.h"
+#include "tree-pretty-print.h"
+#include "analyzer.h"
+#include "graphviz.h"
+#include "cgraph.h"
+#include "digraph.h"
+
+void
+digraph::dump_dot_to_pp (pretty_printer *pp) const
+{
+  pp_string (pp, "digraph \"");
+  pp_write_text_to_stream (pp);
+  pp_string (pp, "FIXME: base");
+  pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
+  pp_string (pp, "\" {\n");
+
+  pp_string (pp, "overlap=false;\n");
+  pp_string (pp, "compound=true;\n");
+
+  // TODO: maybe (optionally) sub-subdivide by TU, for LTO
+
+  /* TODO: see also:
+     https://gcc-python-plugin.readthedocs.io/en/latest/_images/sample-supergraph.png
+  */
+
+  /* Nodes.  */
+  int i;
+  dnode *n;
+  FOR_EACH_VEC_ELT (m_nodes, i, n)
+    n->dump_dot ();
+
+  /* Edges.  */
+  dedge *e;
+  FOR_EACH_VEC_ELT (m_edges, i, e)
+    e->dump_dot ();
+
+  /* Terminate "digraph" */
+  pp_string (pp, "}");
+  pp_newline (pp);
+}
+
+void
+digraph::dump_dot_to_file (FILE *fp) const
+{
+  pretty_printer pp;
+  pp.buffer->stream = fp;
+  dump_dot_to_pp (&pp);
+  pp_flush (&pp);
+}
+
+void
+digraph::dump_dot (const char *path) const
+{
+  FILE *fp = fopen (path, "w");
+  dump_dot_to_file (fp);
+  fclose (fp);
+}
diff --git a/gcc/analyzer/digraph.h b/gcc/analyzer/digraph.h
new file mode 100644
index 0000000..50f5390
--- /dev/null
+++ b/gcc/analyzer/digraph.h
@@ -0,0 +1,59 @@
+/* FIXME.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_ANALYZER_DIGRAPH_H
+#define GCC_ANALYZER_DIGRAPH_H
+
+/* Forwards decls.  */
+class dnode;
+class dedge;
+
+class dnode
+{
+ public:
+  virtual ~dnode () {}
+
+  virtual void dump_dot () const = 0;
+};
+
+class dedge
+{
+ public:
+  virtual ~dedge () {}
+
+  virtual void dump_dot () const = 0;
+};
+
+class digraph
+{
+ public:
+  digraph () {}
+  virtual ~digraph () {}
+
+  void dump_dot_to_pp (pretty_printer *pp) const;
+  void dump_dot_to_file (FILE *fp) const;
+  void dump_dot (const char *path) const;
+
+ private:
+  auto_delete_vec<dnode> m_nodes;
+  auto_delete_vec<dedge> m_edges;
+};
+
+#endif /* GCC_ANALYZER_DIGRAPH_H */
diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 89d747d..0b2ba37 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -41,6 +41,7 @@
 #include "fold-const.h"
 #include "tristate.h"
 #include "selftest.h"
+#include "exploded-graph.h"
 
 ////////////////////////////////////////////////////////////////////////////
 
@@ -480,11 +481,6 @@ char *print (const char *fmt, ...)
   ti.x_data = NULL;
   ti.m_richloc = &rich_loc;
 
-#if 1
-#else
-  pp_show_color (pp) = pp_show_color (global_dc->printer);
-  pp_format_decoder (pp) = pp_format_decoder (global_dc->printer);
-#endif
   pp_format (pp, &ti);
   pp_output_formatted_text (pp);
 
@@ -3453,6 +3449,15 @@ run_one_checker (const supergraph &sg, const state_machine &sm, logger *logger)
       free (filename);
     }
 
+  if (1)
+    {
+      exploded_graph eg;
+      char *filename
+	= concat (dump_base_name, ".", sm.get_name (), ".eg.dot", NULL);
+      eg.dump_dot (filename);
+      free (filename);
+    }
+
   if (logger)
     {
       log_scope s (logger, "per-node state");
diff --git a/gcc/analyzer/exploded-graph.cc b/gcc/analyzer/exploded-graph.cc
new file mode 100644
index 0000000..d4771dd
--- /dev/null
+++ b/gcc/analyzer/exploded-graph.cc
@@ -0,0 +1,45 @@
+#include "config.h"
+#include "gcc-plugin.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "tm.h"
+#include "toplev.h"
+#include "hash-table.h"
+#include "vec.h"
+#include "ggc.h"
+#include "basic-block.h"
+#include "tree-ssa-alias.h"
+#include "internal-fn.h"
+#include "gimple-fold.h"
+#include "tree-eh.h"
+#include "gimple-expr.h"
+#include "is-a.h"
+#include "gimple.h"
+#include "tree-pass.h"
+#include "intl.h"
+#include "context.h"
+#include "diagnostic.h"
+#include "bitmap.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "gimple-pretty-print.h"
+#include "tree-pretty-print.h"
+#include "analyzer.h"
+#include "graphviz.h"
+#include "cgraph.h"
+#include "exploded-graph.h"
+
+void
+exploded_node::dump_dot () const
+{
+}
+
+void
+exploded_edge::dump_dot () const
+{
+}
+
+exploded_graph::exploded_graph ()
+{
+}
diff --git a/gcc/analyzer/exploded-graph.h b/gcc/analyzer/exploded-graph.h
new file mode 100644
index 0000000..1f71dcf
--- /dev/null
+++ b/gcc/analyzer/exploded-graph.h
@@ -0,0 +1,59 @@
+/* FIXME.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_ANALYZER_EXPLODED_GRAPH_H
+#define GCC_ANALYZER_EXPLODED_GRAPH_H
+
+#include "digraph.h"
+#include "supergraph.h"
+#include "hardcoded-state-machine.h"
+
+class exploded_node : public dnode
+{
+ public:
+  exploded_node (const supernode *supernode,
+		 tree var,
+		 state_machine::state_t state)
+  : m_supernode (supernode),
+    m_var (var),
+    m_state (state)
+  {
+  }
+
+  void dump_dot () const FINAL OVERRIDE;
+
+  const supernode *m_supernode;
+  tree m_var;
+  state_machine::state_t m_state;
+};
+
+class exploded_edge : public dedge
+{
+ public:
+  void dump_dot () const FINAL OVERRIDE;
+};
+
+class exploded_graph : public digraph
+{
+ public:
+  exploded_graph ();
+};
+
+#endif /* GCC_ANALYZER_EXPLODED_GRAPH_H */
diff --git a/gcc/analyzer/hardcoded-state-machine.h b/gcc/analyzer/hardcoded-state-machine.h
index 0d3992b..72a97c9 100644
--- a/gcc/analyzer/hardcoded-state-machine.h
+++ b/gcc/analyzer/hardcoded-state-machine.h
@@ -1,3 +1,26 @@
+/* FIXME.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+   Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_ANALYZER_HARDCODED_STATE_MACHINE_H
+#define GCC_ANALYZER_HARDCODED_STATE_MACHINE_H
+
 #include "analyzer-logging.h"
 #include "diagnostic-metadata.h"
 
@@ -289,3 +312,5 @@ public:
 private:
   state_t m_start;
 };
+
+#endif /* GCC_ANALYZER_HARDCODED_STATE_MACHINE_H */
-- 
1.8.5.3

