From 2aa12000d5a52b374dcd3778d0019e798e19b335 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 20 May 2020 05:54:47 -0400
Subject: [PATCH 160/293] FIXME: simplify dumping of regions with a simple
 directly-bound value

---
 gcc/analyzer/region-model2.cc |  3 +-
 gcc/analyzer/store2.cc        | 60 ++++++++++++++++++++++++++++++++---
 gcc/analyzer/store2.h         |  5 ++-
 3 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/gcc/analyzer/region-model2.cc b/gcc/analyzer/region-model2.cc
index ce703a6b6f3..1da9ea27784 100644
--- a/gcc/analyzer/region-model2.cc
+++ b/gcc/analyzer/region-model2.cc
@@ -3591,7 +3591,8 @@ region_model2::dump_to_pp (pretty_printer *pp, bool simple,
   /* Dump store.  */
   if (!multiline)
     pp_string (pp, ", {");
-  m_store.dump_to_pp (pp, simple, multiline);
+  m_store.dump_to_pp (pp, simple, multiline,
+		      m_mgr->get_store2_manager ());
   if (!multiline)
     pp_string (pp, "}");
 
diff --git a/gcc/analyzer/store2.cc b/gcc/analyzer/store2.cc
index be2668ebad2..ae1a23cf19d 100644
--- a/gcc/analyzer/store2.cc
+++ b/gcc/analyzer/store2.cc
@@ -273,6 +273,8 @@ binding_cluster2::operator== (const binding_cluster2 &other) const
   return true;
 }
 
+/* FIXME.  */
+
 void
 binding_cluster2::dump_to_pp (pretty_printer *pp, bool simple,
 			      bool multiline) const
@@ -669,6 +671,25 @@ binding_cluster2::get_any_value (const binding_key2 *key) const
   return NULL;
 }
 
+/* If this cluster has a single direct binding for the whole of the region,
+   return it.
+   For use in simplifying dumps.  */
+
+const svalue2 *
+binding_cluster2::maybe_get_simple_value (store2_manager *mgr) const
+{
+  /* Fail gracefully if MGR is NULL to make it easier to dump store2
+     instances in the debugger.  */
+  if (mgr == NULL)
+    return NULL;
+
+  if (m_map.elements () != 1)
+    return NULL;
+
+  const binding_key2 *key = binding_key2::make (mgr, m_base_region, BK_direct);
+  return get_any_value (key);
+}
+
 /* class store2_manager.  */
 
 /* binding consolidation.  */
@@ -806,10 +827,13 @@ get_sorted_parent_regions (auto_vec<const region2 *> *out,
   out->qsort (region2::cmp_ptrs);
 }
 
-/* FIXME.  */
+/* FIXME.
+   MGR is used for simplifying dumps if non-NULL, but can also be NULL
+   (to make it easier to use from the debugger).  */
 
 void
-store2::dump_to_pp (pretty_printer *pp, bool simple, bool multiline) const
+store2::dump_to_pp (pretty_printer *pp, bool simple, bool multiline,
+		    store2_manager *mgr) const
 {
   /* Sort into some deterministic order.  */
   auto_vec<const region2 *> base_regions;
@@ -847,7 +871,35 @@ store2::dump_to_pp (pretty_printer *pp, bool simple, bool multiline) const
 	    continue;
 	  binding_cluster2 *cluster
 	    = *const_cast<cluster_map_t &> (m_cluster_map).get (base_reg);
-	  if (multiline)
+	  if (const svalue2 *sval = cluster->maybe_get_simple_value (mgr))
+	    {
+	      /* Special-case to simplify dumps for the common case where
+		 we just have one value directly bound to the whole of a
+		 region.  */
+	      if (multiline)
+		{
+		  pp_string (pp, "  cluster for: ");
+		  base_reg->dump_to_pp (pp, simple);
+		  pp_string (pp, ": ");
+		  sval->dump_to_pp (pp, simple);
+		  if (cluster->escaped_p ())
+		    pp_string (pp, " (ESCAPED)");
+		  pp_newline (pp);
+		}
+	      else
+		{
+		  if (i > 0)
+		    pp_string (pp, ", ");
+		  pp_string (pp, "region: {");
+		  base_reg->dump_to_pp (pp, simple);
+		  pp_string (pp, ", value: ");
+		  sval->dump_to_pp (pp, simple);
+		  if (cluster->escaped_p ())
+		    pp_string (pp, " (ESCAPED)");
+		  pp_string (pp, "}");
+		}
+	    }
+	  else if (multiline)
 	    {
 	      pp_string (pp, "  cluster for: ");
 	      base_reg->dump_to_pp (pp, simple);
@@ -879,7 +931,7 @@ store2::dump (bool simple) const
   pp_format_decoder (&pp) = default_tree_printer;
   pp_show_color (&pp) = pp_show_color (global_dc->printer);
   pp.buffer->stream = stderr;
-  dump_to_pp (&pp, simple, true);
+  dump_to_pp (&pp, simple, true, NULL);
   pp_newline (&pp);
   pp_flush (&pp);
 }
diff --git a/gcc/analyzer/store2.h b/gcc/analyzer/store2.h
index c73e209e998..c41629160a0 100644
--- a/gcc/analyzer/store2.h
+++ b/gcc/analyzer/store2.h
@@ -237,6 +237,8 @@ public:
 				     const svalue2 *sval,
 				     auto_vec<path_var> *out_pvs) const;
 
+  const svalue2 *maybe_get_simple_value (store2_manager *mgr) const;
+
 private:
   const svalue2 *get_any_value (const binding_key2 *key) const;
 #if 0
@@ -288,7 +290,8 @@ public:
     return !(*this == other);
   }
 
-  void dump_to_pp (pretty_printer *pp, bool summarize, bool multiline) const;
+  void dump_to_pp (pretty_printer *pp, bool summarize, bool multiline,
+		   store2_manager *mgr) const;
   void dump (bool simple) const;
   void summarize_to_pp (pretty_printer *pp, bool simple) const;
 
-- 
2.26.2

