From 113d7fc7691d85bfba6d499fb1e716ed897f40ad Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 13 May 2020 11:37:53 -0400
Subject: [PATCH 121/315] FIXME: add log_stats of managed objects

---
 gcc/analyzer/engine.cc        |   3 +
 gcc/analyzer/region-model2.cc | 101 ++++++++++++++++++++++++++++++++++
 gcc/analyzer/region-model2.h  |   4 ++
 gcc/analyzer/store2.h         |   2 +
 gcc/analyzer/uniq-manager.h   |   3 +
 5 files changed, 113 insertions(+)

diff --git a/gcc/analyzer/engine.cc b/gcc/analyzer/engine.cc
index 11cb2e868ba..524a79f9649 100644
--- a/gcc/analyzer/engine.cc
+++ b/gcc/analyzer/engine.cc
@@ -3551,6 +3551,9 @@ exploded_graph::log_stats () const
 
   LOG_SCOPE (logger);
 
+  if (m_ext_state.get_engine2 ())
+    m_ext_state.get_engine2 ()->log_stats (logger);
+
   logger->log ("m_sg.num_nodes (): %i", m_sg.num_nodes ());
   logger->log ("m_nodes.length (): %i", m_nodes.length ());
   logger->log ("m_edges.length (): %i", m_edges.length ());
diff --git a/gcc/analyzer/region-model2.cc b/gcc/analyzer/region-model2.cc
index 7bddbc77a3f..cdfac4a52a7 100644
--- a/gcc/analyzer/region-model2.cc
+++ b/gcc/analyzer/region-model2.cc
@@ -3240,6 +3240,97 @@ region_model2_manager::create_region_for_alloca (const frame_region2 *frame)
   return reg;
 }
 
+/* FIXME.  */
+
+template <typename T>
+static void
+log_mananged_object (logger *logger, const T *obj)
+{
+  logger->start_log_line ();
+  pretty_printer *pp = logger->get_printer ();
+  pp_string (pp, "    ");
+  obj->dump_to_pp (pp, true);
+  logger->end_log_line ();
+}
+
+/* FIXME.  */
+
+template <typename K, typename T>
+static void
+log_uniq_map (logger *logger, bool show_objs, const char *title,
+	      const hash_map<K, T*> &uniq_map)
+{
+  logger->log ("  # %s: %li", title, uniq_map.elements ());
+  if (show_objs)
+    for (typename hash_map<K, T*>::iterator iter = uniq_map.begin ();
+	 iter != uniq_map.end (); ++iter)
+      {
+	T *managed_obj = (*iter).second;
+	log_mananged_object<T> (logger, managed_obj);
+      }
+}
+
+/* FIXME.  */
+
+template <typename T>
+static void
+log_uniq_manager (logger *logger, bool show_objs, const char *title,
+		  const uniq_manager<T> &uniq_mgr)
+{
+  logger->log ("  # %s: %li", title, uniq_mgr.get_count ());
+  if (show_objs)
+    for (typename uniq_manager<T>::iterator_t iter = uniq_mgr.begin ();
+	 iter != uniq_mgr.end (); ++iter)
+      {
+	T* managed_obj = (*iter).second;
+	log_mananged_object<T> (logger, managed_obj);
+      }
+}
+
+/* FIXME.  */
+
+void
+region_model2_manager::log_stats (logger *logger, bool show_objs) const
+{
+  LOG_SCOPE (logger);
+  logger->log ("svalue2 consolidation");
+  log_uniq_map (logger, show_objs, "constant_svalue2", m_constants_map);
+  log_uniq_map (logger, show_objs, "poisoned_svalue2", m_poisoned_values_map);
+  log_uniq_map (logger, show_objs, "setjmp_svalue2", m_setjmp_values_map);
+  log_uniq_map (logger, show_objs, "initial_svalue2", m_initial_values_map);
+  log_uniq_map (logger, show_objs, "region_svalue2", m_pointer_values_map);
+  log_uniq_map (logger, show_objs, "unaryop_svalue2", m_unaryop_values_map);
+  log_uniq_map (logger, show_objs, "binop_svalue2", m_binop_values_map);
+  log_uniq_map (logger, show_objs, "sub_svalue2", m_sub_values_map);
+  logger->log ("region2 consolidation");
+  logger->log ("  next region id: %i", m_next_region_id);
+  log_uniq_map (logger, show_objs, "function_region2", m_fndecls_map);
+  log_uniq_map (logger, show_objs, "label_region2", m_labels_map);
+  log_uniq_map (logger, show_objs, "decl_region2", m_globals_map);
+  log_uniq_manager (logger, show_objs, "field_region2", m_fields);
+  log_uniq_manager (logger, show_objs, "element_region2", m_elements);
+  log_uniq_manager (logger, show_objs, "offset_region2", m_offset_regions);
+  log_uniq_manager (logger, show_objs, "frame_region2", m_frames);
+  log_uniq_manager (logger, show_objs, "symbolic_region2", m_symbolic_regions);
+  log_uniq_map (logger, show_objs, "string_region2", m_string_map);
+  logger->log ("  # managed dynamic regions: %i",
+	       m_managed_dynamic_regions.length ());
+  m_store2_mgr.log_stats (logger, show_objs);
+}
+
+/* FIXME.  */
+// FIXME: this is here so it can use log_uniq_manager
+
+void
+store2_manager::log_stats (logger *logger, bool show_objs) const
+{
+  LOG_SCOPE (logger);
+  log_uniq_manager (logger, show_objs, "concrete_binding2",
+		    m_concrete_binding_key_mgr);
+  log_uniq_manager (logger, show_objs, "symbolic_binding2",
+		    m_symbolic_binding_key_mgr);
+}
+
 /* class region_model2.  */
 
 /* FIXME: region_model2's default ctor.  */
@@ -7514,6 +7605,16 @@ debug (const region_model2 &rmodel)
   rmodel.dump (false);
 }
 
+/* class engine2.  */
+
+/* FIXME.  */
+
+void
+engine2::log_stats (logger *logger) const
+{
+  m_mgr.log_stats (logger, true);
+}
+
 namespace ana {
 
 #if CHECKING_P
diff --git a/gcc/analyzer/region-model2.h b/gcc/analyzer/region-model2.h
index 709555ba28b..a601452c439 100644
--- a/gcc/analyzer/region-model2.h
+++ b/gcc/analyzer/region-model2.h
@@ -1783,6 +1783,8 @@ public:
   const region2 *create_region_for_heap_alloc ();
   const region2 *create_region_for_alloca (const frame_region2 *frame);
 
+  void log_stats (logger *logger, bool show_objs) const;
+
 private:
   const svalue2 *maybe_fold_unaryop (tree type, enum tree_code op,
 				     const svalue2 *arg);
@@ -2343,6 +2345,8 @@ class engine2
 public:
   region_model2_manager *get_model_manager () { return &m_mgr; }
 
+  void log_stats (logger *logger) const;
+
 private:
   region_model2_manager m_mgr;
 
diff --git a/gcc/analyzer/store2.h b/gcc/analyzer/store2.h
index 6f05bafe111..4b3dd3bc45e 100644
--- a/gcc/analyzer/store2.h
+++ b/gcc/analyzer/store2.h
@@ -341,6 +341,8 @@ public:
     return m_mgr;
   }
 
+  void log_stats (logger *logger, bool show_objs) const;
+
 private:
   region_model2_manager *m_mgr;
   uniq_manager<concrete_binding2> m_concrete_binding_key_mgr;
diff --git a/gcc/analyzer/uniq-manager.h b/gcc/analyzer/uniq-manager.h
index 8a6451bb641..16076522e5a 100644
--- a/gcc/analyzer/uniq-manager.h
+++ b/gcc/analyzer/uniq-manager.h
@@ -173,6 +173,9 @@ class uniq_manager
   /* How many T are being managed.  */
   size_t get_count () const { return m_map.elements (); }
 
+  iterator_t begin () const { return m_map.begin (); }
+  iterator_t end () const { return m_map.end (); }
+
  private:
   // FIXME: should this just be a hash_set?
   map_t m_map;
-- 
2.26.2

