From a09f96b5d24221f0121db69c25d33e1ebe1716a1 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 Mar 2019 15:28:58 -0500
Subject: [PATCH 079/169] FIXME: capture per-stmt data

---
 gcc/analyzer/engine.cc     | 41 +++++++++++++++++++++++++++++++++++++++++
 gcc/analyzer/supergraph.cc |  4 ++++
 gcc/analyzer/supergraph.h  |  3 +++
 3 files changed, 48 insertions(+)

diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 63cbc27..f3dfc8ef 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -335,6 +335,21 @@ struct per_node_t
   set_of_summary_edges m_sfx_transition;
 };
 
+struct per_stmt_t
+{
+  void add_transition (tree var,
+		       state_machine::state_t from,
+		       state_machine::state_t to)
+  {
+    // FIXME: bogus global states here
+    summary_edge e (summary_point (0, var_state_t (var, from)),
+		    summary_point (0, var_state_t (var, to)));
+    m_transitions.add (e);
+  }
+
+  set_of_summary_edges m_transitions;
+};
+
 /* FIXME.  */
 
 char *print (const char *fmt, ...)
@@ -717,6 +732,7 @@ public:
   engine (const supergraph &sg, const state_machine &sm,
 	  logger *logger, int verbosity)
   : log_user (logger), m_sg (sg), m_sm (sm), m_per_node_data (),
+    m_per_stmt_data (),
     m_verbosity (verbosity),
     m_candidate_diagnostics (),
     m_num_traverse_calls (0),
@@ -767,6 +783,15 @@ public:
     return *data;
   }
 
+  per_stmt_t &get_data_for_stmt (const gimple *stmt)
+  {
+    if (per_stmt_t **ptr_ptr = m_per_stmt_data.get (stmt))
+      return **ptr_ptr;
+    per_stmt_t *data = new per_stmt_t (); // FIXME: can we make it not be indirect?
+    m_per_stmt_data.put (stmt, data);
+    return *data;
+  }
+
   void log_stats () const;
 
   void detect_leaks (const supernode *node, const sm_instance &sm_inst,
@@ -790,6 +815,8 @@ public:
   // FIXME: should this be just a vec, instead (using the node indices)?
   hash_map <const supernode *, per_node_t *> m_per_node_data;
 
+  hash_map <const gimple *, per_stmt_t *> m_per_stmt_data;
+
   int m_verbosity;
 
   auto_delete_vec <candidate_diagnostic> m_candidate_diagnostics;
@@ -997,6 +1024,9 @@ public:
 					var, from, to);
 	    m_path->add_event (event);
 	  }
+
+	per_stmt_t &stmt_data = m_engine->get_data_for_stmt (stmt);
+	stmt_data.add_transition (var, from, to);
       }
 
     if (m_engine->get_logger ())
@@ -2345,6 +2375,17 @@ public:
     pp_flush (pp);
   }
 
+  void add_stmt_annotations (pretty_printer *pp,
+			     const gimple *stmt) const FINAL OVERRIDE
+  {
+    per_stmt_t &data
+      = const_cast <engine &> (m_engine).get_data_for_stmt (stmt);
+
+    pp_newline (pp);
+    pp_printf (pp, "  transitions: %s",
+	       data.m_transitions.to_str (m_engine.m_sm).c_str ());
+  }
+
 private:
   const engine &m_engine;
 };
diff --git a/gcc/analyzer/supergraph.cc b/gcc/analyzer/supergraph.cc
index 0e0dd7f..ab485b2 100644
--- a/gcc/analyzer/supergraph.cc
+++ b/gcc/analyzer/supergraph.cc
@@ -388,6 +388,8 @@ supernode::dump_dot (graphviz_out *gv,
     {
       pp_string (pp, "returning call: ");
       pp_gimple_stmt_1 (pp, m_returning_call, 0, (dump_flags_t)0);
+      if (a)
+	a->add_stmt_annotations (pp, m_returning_call);
       pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
       pp_newline (pp);
     }
@@ -420,6 +422,8 @@ supernode::dump_dot (graphviz_out *gv,
   FOR_EACH_VEC_ELT (m_stmts, i, stmt)
     {
       pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0);
+      if (a)
+	a->add_stmt_annotations (pp, stmt);
       pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
       pp_newline (pp);
     }
diff --git a/gcc/analyzer/supergraph.h b/gcc/analyzer/supergraph.h
index 0d8901d..52f86d2 100644
--- a/gcc/analyzer/supergraph.h
+++ b/gcc/analyzer/supergraph.h
@@ -306,6 +306,9 @@ class dot_annotator
   virtual void add_node_annotations (pretty_printer *pp ATTRIBUTE_UNUSED,
 				     const supernode &n ATTRIBUTE_UNUSED)
     const {}
+  virtual void add_stmt_annotations (pretty_printer *pp ATTRIBUTE_UNUSED,
+				     const gimple *stmt ATTRIBUTE_UNUSED)
+    const {}
 };
 
 extern cgraph_edge *supergraph_call_edge (function *fun, gimple *stmt);
-- 
1.8.5.3

