From 07183a6d2c5573f55beaeb310b64e2095e3522ab Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 5 Mar 2021 14:31:51 -0500
Subject: [PATCH 26/30] FIXME: WIP on trimmed_graph for feasbibility

---
 gcc/Makefile.in                    |   3 +-
 gcc/analyzer/diagnostic-manager.cc |  47 +++++++++
 gcc/analyzer/trimmed-graph.cc      | 164 +++++++++++++++++++++++++++++
 gcc/analyzer/trimmed-graph.h       | 103 ++++++++++++++++++
 4 files changed, 316 insertions(+), 1 deletion(-)
 create mode 100644 gcc/analyzer/trimmed-graph.cc
 create mode 100644 gcc/analyzer/trimmed-graph.h

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index a63c5d9cab6..215d0aa5c4f 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1273,7 +1273,8 @@ ANALYZER_OBJS = \
 	analyzer/state-purge.o \
 	analyzer/store.o \
 	analyzer/supergraph.o \
-	analyzer/svalue.o
+	analyzer/svalue.o \
+	analyzer/trimmed-graph.o
 
 # Language-independent object files.
 # We put the *-match.o and insn-*.o files first so that a parallel make
diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index 1a3dd8c7081..4e54d388efe 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -57,6 +57,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "analyzer/supergraph.h"
 #include "analyzer/program-state.h"
 #include "analyzer/exploded-graph.h"
+#include "analyzer/trimmed-graph.h"
 #include "analyzer/checker-path.h"
 #include "analyzer/reachability.h"
 
@@ -84,6 +85,9 @@ public:
 				 feasibility_problem **out_problem);
 
 private:
+  void trimmed_graph_experiment (const exploded_node *enode,
+				 const char *desc);
+
   exploded_path *get_best_concat_epath (const exploded_node *enode);
   exploded_path *get_concat_epath_via_to (const exploded_node *via_enode,
 					  const exploded_node *final_enode);
@@ -94,6 +98,47 @@ private:
 
 /* class epath_finder.  */
 
+void
+epath_finder::trimmed_graph_experiment (const exploded_node *enode,
+					const char *desc)
+{
+  trimmed_graph tg (m_eg, enode);
+  {
+    auto_timevar tv (TV_ANALYZER_DUMP);
+    static int dump_idx; // FIXME
+    exploded_graph::dump_args_t inner_args (m_eg);
+    trimmed_graph::dump_args_t args (inner_args);
+    pretty_printer pp;
+    pp_printf (&pp, "%s.%i.to-en%i.tg.dot",
+	       dump_base_name, dump_idx++, enode->m_index);
+    char *filename = xstrdup (pp_formatted_text (&pp));
+    //root_cluster c;
+    tg.dump_dot (filename, NULL, args);
+    // annotations!
+    // capture which edges are in the path and highlight!
+    // capture which nodes are in the path and highlight!
+    // capture state at each point along the path
+    free (filename);
+  }
+
+  /* IDEA: brute force it on the trimmed graph...
+     Run Dijkstra's alg on the trimmed graph to give an ordering to the nodes.
+     Then iteratively generate paths, searching for all possible paths from
+     origin to destination, favoring shortest ones first.
+     Track how many have been tested so far
+     Have a cutoff param; give up after that many paths.
+     Check feasibility each time.
+     In fact, track state and check feasibility at each edge, so we're
+     always checking feasible path prefixes.
+
+     But that doesn't necessarily make it easier to debug.
+
+     Maybe walk backwards from destination, finding the decisions points
+     i.e. where there were multiple in-edges.
+     Try to find shortest feasible paths to those.  */
+}
+
+
 /* Get the "best" exploded_path for reaching ENODE from the origin,
    returning ownership of it to the caller.
 
@@ -132,6 +177,8 @@ epath_finder::get_best_epath (const exploded_node *enode,
      as there could be longer feasible paths within the egraph.
      (PR analyzer/96374).  */
 
+  trimmed_graph_experiment (enode, desc);
+
   exploded_path *epath = new exploded_path (m_sep.get_shortest_path (enode));
   if (epath->feasible_p (logger, out_problem, m_eg.get_engine (), &m_eg))
     {
diff --git a/gcc/analyzer/trimmed-graph.cc b/gcc/analyzer/trimmed-graph.cc
new file mode 100644
index 00000000000..5ea5c1d2207
--- /dev/null
+++ b/gcc/analyzer/trimmed-graph.cc
@@ -0,0 +1,164 @@
+/* Trimming an exploded graph to a subset of nodes and edges.
+   Copyright (C) 2021 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/>.  */
+
+// TODO: how many of these do we actually need?
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "pretty-print.h"
+#include "gcc-rich-location.h"
+#include "gimple-pretty-print.h"
+#include "function.h"
+#include "diagnostic-core.h"
+#include "diagnostic-event-id.h"
+#include "diagnostic-path.h"
+#include "alloc-pool.h"
+#include "fibonacci_heap.h"
+#include "shortest-paths.h"
+#include "sbitmap.h"
+#include "bitmap.h"
+#include "tristate.h"
+#include "selftest.h"
+#include "ordered-hash-map.h"
+#include "json.h"
+#include "analyzer/analyzer.h"
+#include "analyzer/analyzer-logging.h"
+#include "analyzer/sm.h"
+#include "analyzer/pending-diagnostic.h"
+#include "analyzer/diagnostic-manager.h"
+#include "analyzer/call-string.h"
+#include "analyzer/program-point.h"
+#include "analyzer/store.h"
+#include "analyzer/region-model.h"
+#include "analyzer/constraint-manager.h"
+#include "cfg.h"
+#include "basic-block.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "cgraph.h"
+#include "digraph.h"
+#include "analyzer/supergraph.h"
+#include "analyzer/program-state.h"
+#include "analyzer/exploded-graph.h"
+#include "analyzer/trimmed-graph.h"
+
+#if ENABLE_ANALYZER
+
+namespace ana {
+
+/* class trimmed_node : public dnode<tg_traits>.  */
+
+/* FIXME.  */
+
+void
+trimmed_node::dump_dot (graphviz_out *gv,
+			const dump_args_t &args) const
+{
+  m_inner_node->dump_dot (gv, args.m_inner_args);
+}
+
+/* class trimmed_edge : public dedge<tg_traits>.  */
+
+/* FIXME.  */
+
+trimmed_edge::trimmed_edge (trimmed_node *src, trimmed_node *dest,
+			    const exploded_edge *inner_edge)
+: dedge<tg_traits> (src, dest), m_inner_edge (inner_edge)
+{
+}
+
+/* FIXME.  */
+
+void
+trimmed_edge::dump_dot (graphviz_out *gv, const dump_args_t &args) const
+{
+  m_inner_edge->dump_dot (gv, args.m_inner_args);
+}
+
+/* class trimmed_graph : public digraph <tg_traits>.  */
+
+/* Ctor for trimmed_graph: construct a graph equivalent to trimming
+   INNER_GRAPH to all nodes that can reach INNER_DST_NODE.  */
+
+trimmed_graph::trimmed_graph (const exploded_graph &inner_graph,
+			      const exploded_node *inner_dst_node)
+{
+  /* Determine what subset of nodes and edges to include in the
+     trimmed graph.
+     Walk backwards from INNER_DST_NODE, finding nodes that reach it,
+     iteratively building the set of nodes and edges.  */
+  hash_set <const exploded_node *> enodes;
+  hash_set <const exploded_edge *> eedges;
+  auto_vec <const exploded_node *> worklist;
+  worklist.safe_push (inner_dst_node);
+  enodes.add (inner_dst_node);
+  while (worklist.length () > 0)
+    {
+      const exploded_node *inner_node = worklist.pop ();
+      exploded_edge *inner_pred;
+      unsigned i;
+      FOR_EACH_VEC_ELT (inner_node->m_preds, i, inner_pred)
+	{
+	  if (!enodes.contains (inner_pred->m_src))
+	    {
+	      worklist.safe_push (inner_pred->m_src);
+	      enodes.add (inner_pred->m_src);
+	    }
+	  eedges.add (inner_pred);
+	}
+    }
+
+  /* Create trimmed nodes for all enodes in the set.  */
+  {
+    /* Do it this way to ensure deterministic order.  */
+    exploded_node *inner_node;
+    unsigned i;
+    FOR_EACH_VEC_ELT (inner_graph.m_nodes, i, inner_node)
+      if (enodes.contains (inner_node))
+	{
+	  trimmed_node *tnode = new trimmed_node (inner_node);
+	  add_node (tnode);
+	  m_map_from_enode_to_tnode.put (inner_node, tnode);
+	}
+  }
+
+  /* Create trimmed edges for all edges in the set.  */
+  {
+    /* Do it this way to ensure deterministic order.  */
+    exploded_edge *inner_edge;
+    unsigned i;
+    FOR_EACH_VEC_ELT (inner_graph.m_edges, i, inner_edge)
+      if (eedges.contains (inner_edge))
+	{
+	  const exploded_node *inner_src = inner_edge->m_src;
+	  const exploded_node *inner_dest = inner_edge->m_dest;
+	  trimmed_node *tsrc = *m_map_from_enode_to_tnode.get (inner_src);
+	  trimmed_node *tdest = *m_map_from_enode_to_tnode.get (inner_dest);
+	  trimmed_edge *tedge = new trimmed_edge (tsrc, tdest, inner_edge);
+	  add_edge (tedge);
+	}
+  }
+}
+
+} // namespace ana
+
+#endif /* #if ENABLE_ANALYZER */
diff --git a/gcc/analyzer/trimmed-graph.h b/gcc/analyzer/trimmed-graph.h
new file mode 100644
index 00000000000..685ad86ed9a
--- /dev/null
+++ b/gcc/analyzer/trimmed-graph.h
@@ -0,0 +1,103 @@
+/* Trimming an exploded graph to a subset of nodes and edges.
+   Copyright (C) 2021 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_TRIMMED_GRAPH_H
+#define GCC_ANALYZER_TRIMMED_GRAPH_H
+
+namespace ana {
+
+/* Forward decls.  */
+
+class trimmed_node;
+class trimmed_edge;
+class trimmed_graph;
+class trimmed_cluster;
+
+/* A traits class for trimming a digraph to a subset of nodes and edges.  */
+
+struct tg_traits
+{
+  typedef trimmed_node node_t;
+  typedef trimmed_edge edge_t;
+  typedef trimmed_graph graph_t;
+  struct dump_args_t
+  {
+    typedef typename eg_traits::dump_args_t inner_args_t;
+
+    dump_args_t (const inner_args_t &inner_args)
+    : m_inner_args (inner_args)
+    {
+    }
+
+    const inner_args_t &m_inner_args;
+  };
+  typedef trimmed_cluster cluster_t;
+};
+
+/* FIXME.  */
+
+class trimmed_node : public dnode<tg_traits>
+{
+public:
+  trimmed_node (const exploded_node *inner_node)
+  : m_inner_node (inner_node) {}
+
+  void dump_dot (graphviz_out *gv,
+		 const dump_args_t &args) const FINAL OVERRIDE;
+
+private:
+  const exploded_node *m_inner_node;
+};
+
+/* FIXME.  */
+
+class trimmed_edge : public dedge<tg_traits>
+{
+ public:
+  trimmed_edge (trimmed_node *src, trimmed_node *dest,
+		const exploded_edge *inner_edge);
+
+  void dump_dot (graphviz_out *gv,
+		 const dump_args_t &args) const FINAL OVERRIDE;
+
+ private:
+  const exploded_edge *m_inner_edge;
+};
+
+/* FIXME.  */
+
+class trimmed_graph : public digraph <tg_traits>
+{
+ public:
+  trimmed_graph (const exploded_graph &inner_graph,
+		 const exploded_node *inner_dst_node);
+
+ private:
+  typedef hash_map<const exploded_node *, trimmed_node *> map_t;
+  map_t m_map_from_enode_to_tnode;
+};
+
+class trimmed_cluster : public cluster <tg_traits>
+{
+};
+
+} // namespace ana
+
+#endif /* GCC_ANALYZER_TRIMMED_GRAPH_H */
-- 
2.26.2

