From 3348806a47fa9e8921aabc7cc91556364d18bdd2 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Sun, 26 Apr 2020 19:03:34 -0400
Subject: [PATCH 076/293] FIXME: group by parent region when dumping store

---
 gcc/analyzer/region-model2.cc | 12 ++---
 gcc/analyzer/store2.cc        | 84 ++++++++++++++++++++++++++---------
 2 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/gcc/analyzer/region-model2.cc b/gcc/analyzer/region-model2.cc
index d95b1e87921..ad12526d643 100644
--- a/gcc/analyzer/region-model2.cc
+++ b/gcc/analyzer/region-model2.cc
@@ -2281,8 +2281,7 @@ void
 code_region2::dump_to_pp (pretty_printer *pp, bool simple) const
 {
   if (simple)
-    {
-    }
+    pp_string (pp, "code region");
   else
     pp_string (pp, "code_region2()");
 }
@@ -2331,8 +2330,7 @@ void
 stack_region2::dump_to_pp (pretty_printer *pp, bool simple) const
 {
   if (simple)
-    {
-    }
+    pp_string (pp, "stack region");
   else
     pp_string (pp, "stack_region2()");
 }
@@ -2504,8 +2502,7 @@ void
 heap_region2::dump_to_pp (pretty_printer *pp, bool simple) const
 {
   if (simple)
-    {
-    }
+    pp_string (pp, "heap region");
   else
     pp_string (pp, "heap_region2()");
 }
@@ -2543,8 +2540,7 @@ void
 root_region2::dump_to_pp (pretty_printer *pp, bool simple) const
 {
   if (simple)
-    {
-    }
+    pp_string (pp, "root region");
   else
     pp_string (pp, "root_region2()");
 }
diff --git a/gcc/analyzer/store2.cc b/gcc/analyzer/store2.cc
index 8787b8e5f0e..2a82b3f00fa 100644
--- a/gcc/analyzer/store2.cc
+++ b/gcc/analyzer/store2.cc
@@ -182,11 +182,11 @@ binding_cluster2::dump_to_pp (pretty_printer *pp, bool simple,
       svalue2 *value = *const_cast <map_t &> (m_map).get (key);
       if (multiline)
 	{
-	  pp_string (pp, "  key:   {");
+	  pp_string (pp, "    key:   {");
 	  key->dump_to_pp (pp, simple);
 	  pp_string (pp, "}");
 	  pp_newline (pp);
-	  pp_string (pp, "  value: ");
+	  pp_string (pp, "    value: ");
 	  if (tree t = value->get_type ())
 	    dump_quoted_tree (pp, t);
 	  pp_string (pp, " {");
@@ -366,6 +366,30 @@ store2::operator== (const store2 &other) const
   return true;
 }
 
+/* FIXME.  */
+
+static void
+get_sorted_parent_regions (auto_vec<const region2 *> *out,
+			   auto_vec<const region2 *> &in)
+{
+  /* Get the set of parent regions.  */
+  hash_set<const region2 *> parent_regions;
+  const region2 *iter_reg;
+  unsigned i;
+  FOR_EACH_VEC_ELT (in, i, iter_reg)
+    parent_regions.add (iter_reg->get_parent_region ());
+
+  /* Write to OUT.  */
+  for (hash_set<const region2 *>::iterator iter = parent_regions.begin();
+       iter != parent_regions.end(); ++iter)
+    out->safe_push (*iter);
+
+  /* Sort OUT.  */
+  out->qsort (region2::cmp_ptrs);
+}
+
+/* FIXME.  */
+
 void
 store2::dump_to_pp (pretty_printer *pp, bool summarize, bool multiline) const
 {
@@ -379,28 +403,46 @@ store2::dump_to_pp (pretty_printer *pp, bool summarize, bool multiline) const
     }
   base_regions.qsort (region2::cmp_ptrs);
 
-  const region2 *base_reg;
+  /* Gather clusters, organize by parent region, so that we can group
+     together locals, globals, etc.  */
+  auto_vec<const region2 *> parent_regions;
+  get_sorted_parent_regions (&parent_regions, base_regions);
+
+  const region2 *parent_reg;
   unsigned i;
-  FOR_EACH_VEC_ELT (base_regions, i, base_reg)
+  FOR_EACH_VEC_ELT (parent_regions, i, parent_reg)
     {
-      binding_cluster2 *cluster
-	= *const_cast<cluster_map_t &> (m_cluster_map).get (base_reg);
-      if (multiline)
+      gcc_assert (parent_reg);
+      pp_string (pp, "clusters within ");
+      parent_reg->dump_to_pp (pp, summarize);
+      pp_newline (pp);
+
+      const region2 *base_reg;
+      unsigned j;
+      FOR_EACH_VEC_ELT (base_regions, j, base_reg)
 	{
-	  pp_string (pp, "cluster for: ");
-	  base_reg->dump_to_pp (pp, summarize);
-	  pp_newline (pp);
-	  cluster->dump_to_pp (pp, summarize, multiline);
-	}
-      else
-	{
-	  if (i > 0)
-	    pp_string (pp, ", ");
-	  pp_string (pp, "base region: {");
-	  base_reg->dump_to_pp (pp, summarize);
-	  pp_string (pp, "} has cluster: {");
-	  cluster->dump_to_pp (pp, summarize, multiline);
-	  pp_string (pp, "}");
+	  /* This is O(N * M), but N ought to be small.  */
+	  if (base_reg->get_parent_region () != parent_reg)
+	    continue;
+	  binding_cluster2 *cluster
+	    = *const_cast<cluster_map_t &> (m_cluster_map).get (base_reg);
+	  if (multiline)
+	    {
+	      pp_string (pp, "  cluster for: ");
+	      base_reg->dump_to_pp (pp, summarize);
+	      pp_newline (pp);
+	      cluster->dump_to_pp (pp, summarize, multiline);
+	    }
+	  else
+	    {
+	      if (i > 0)
+		pp_string (pp, ", ");
+	      pp_string (pp, "base region: {");
+	      base_reg->dump_to_pp (pp, summarize);
+	      pp_string (pp, "} has cluster: {");
+	      cluster->dump_to_pp (pp, summarize, multiline);
+	      pp_string (pp, "}");
+	    }
 	}
     }
 }
-- 
2.26.2

