From 9aa1a58d6ed0d5fc38fed96d64277e8fff624472 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 Oct 2019 08:52:59 -0400
Subject: [PATCH 61/63] FIXME: use pool allocators

---
 gcc/analyzer/analyzer.h            | 20 ++++++++++++++++++++
 gcc/analyzer/constraint-manager.cc |  3 +++
 gcc/analyzer/constraint-manager.h  |  3 +++
 gcc/analyzer/region-model.cc       | 23 +++++++++++++++++++++++
 gcc/analyzer/region-model.h        | 30 ++++++++++++++++++++++++++++++
 5 files changed, 79 insertions(+)

diff --git a/gcc/analyzer/analyzer.h b/gcc/analyzer/analyzer.h
index 59fa3c2..f0c6bb0 100644
--- a/gcc/analyzer/analyzer.h
+++ b/gcc/analyzer/analyzer.h
@@ -54,4 +54,24 @@ public:
 #define POP_IGNORE_WFORMAT \
   _Pragma("GCC diagnostic pop")
 
+/* Implement an optimized allocator for TYPENAME objects of fixed size,
+   without requiring a default ctor.  */
+
+#define IMPL_POOL_ALLOCATOR(TYPENAME) \
+  static pool_allocator TYPENAME##_pool (#TYPENAME, sizeof (TYPENAME)); \
+									\
+  void *								\
+  TYPENAME::operator new (size_t sz)					\
+  {									\
+    gcc_assert (sz == sizeof (TYPENAME));				\
+    return TYPENAME##_pool.allocate ();					\
+  }									\
+									\
+  void									\
+  TYPENAME::operator delete (void* ptr, size_t sz)			\
+  {									\
+    gcc_assert (sz == sizeof (TYPENAME));				\
+    TYPENAME##_pool.remove (ptr);					\
+  }
+
 #endif /* GCC_ANALYZER_ANALYZER_H */
diff --git a/gcc/analyzer/constraint-manager.cc b/gcc/analyzer/constraint-manager.cc
index 12846f2..1d112cf 100644
--- a/gcc/analyzer/constraint-manager.cc
+++ b/gcc/analyzer/constraint-manager.cc
@@ -26,6 +26,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "gimple-iterator.h"
 #include "graphviz.h"
+#include "alloc-pool.h"
 #include "selftest.h"
 #include "analyzer/analyzer.h"
 #include "analyzer/supergraph.h"
@@ -152,6 +153,8 @@ range::constrained_to_single_element (tree *out)
 
 /* class equiv_class.  */
 
+IMPL_POOL_ALLOCATOR (equiv_class)
+
 /* equiv_class's default ctor.  */
 
 equiv_class::equiv_class ()
diff --git a/gcc/analyzer/constraint-manager.h b/gcc/analyzer/constraint-manager.h
index 7710dc8..c883d47 100644
--- a/gcc/analyzer/constraint-manager.h
+++ b/gcc/analyzer/constraint-manager.h
@@ -41,6 +41,9 @@ public:
 class equiv_class
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   equiv_class ();
   equiv_class (const equiv_class &other);
 
diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc
index ef16144..1ec7331 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-dfa.h"
 #include "stringpool.h"
 #include "selftest.h"
+#include "alloc-pool.h"
 #include "diagnostic-color.h"
 #include "diagnostic-metadata.h"
 #include "analyzer/analyzer.h"
@@ -326,6 +327,10 @@ svalue::walk_for_canonicalization (canonicalization *) const
 
 /* class region_svalue : public svalue.  */
 
+/* Optimized allocator for region_svalue instances.  */
+
+IMPL_POOL_ALLOCATOR (region_svalue)
+
 /* Compare the fields of this region_svalue with OTHER, returning true
    if they are equal.
    For use by svalue::operator==.  */
@@ -624,6 +629,8 @@ region_svalue::eval_condition (region_svalue *lhs,
 
 /* class constant_svalue : public svalue.  */
 
+IMPL_POOL_ALLOCATOR (constant_svalue)
+
 /* Compare the fields of this constant_svalue with OTHER, returning true
    if they are equal.
    For use by svalue::operator==.  */
@@ -753,6 +760,8 @@ POP_IGNORE_WFORMAT
 
 /* class unknown_svalue : public svalue.  */
 
+IMPL_POOL_ALLOCATOR (unknown_svalue)
+
 /* Compare the fields of this unknown_svalue with OTHER, returning true
    if they are equal.
    For use by svalue::operator==.  */
@@ -804,6 +813,8 @@ poison_kind_to_str (enum poison_kind kind)
 
 /* class poisoned_svalue : public svalue.  */
 
+IMPL_POOL_ALLOCATOR (poisoned_svalue)
+
 /* Compare the fields of this poisoned_svalue with OTHER, returning true
    if they are equal.
    For use by svalue::operator==.  */
@@ -1677,6 +1688,8 @@ map_region::get_value_by_name (tree identifier,
 
 /* class frame_region : public map_region.  */
 
+IMPL_POOL_ALLOCATOR (frame_region)
+
 /* Compare the fields of this frame_region with OTHER, returning true
    if they are equal.
    For use by region::operator==.  */
@@ -1724,6 +1737,8 @@ frame_region::add_to_hash (inchash::hash &hstate) const
 
 /* class stack_region : public region.  */
 
+IMPL_POOL_ALLOCATOR (stack_region)
+
 /* stack_region's copy ctor.  */
 
 stack_region::stack_region (const stack_region &other)
@@ -1964,6 +1979,8 @@ stack_region::get_value_by_name (tree identifier,
 
 /* class heap_region : public region.  */
 
+IMPL_POOL_ALLOCATOR (heap_region)
+
 /* heap_region's copy ctor.  */
 
 heap_region::heap_region (const heap_region &other)
@@ -2025,6 +2042,8 @@ heap_region::walk_for_canonicalization (canonicalization *) const
 
 /* class root_region : public region.  */
 
+IMPL_POOL_ALLOCATOR (root_region)
+
 /* root_region's default ctor.  */
 
 root_region::root_region ()
@@ -2393,6 +2412,8 @@ root_region::get_value_by_name (tree identifier,
 
 /* class symbolic_region : public map_region.  */
 
+IMPL_POOL_ALLOCATOR (symbolic_region)
+
 /* symbolic_region's copy ctor.  */
 
 symbolic_region::symbolic_region (const symbolic_region &other)
@@ -2423,6 +2444,8 @@ symbolic_region::clone () const
 
 /* class region_model.  */
 
+IMPL_POOL_ALLOCATOR (region_model)
+
 /* region_model's default ctor.  */
 
 region_model::region_model ()
diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h
index 72607c7..f8c1111 100644
--- a/gcc/analyzer/region-model.h
+++ b/gcc/analyzer/region-model.h
@@ -513,6 +513,9 @@ public:
 class region_svalue : public svalue
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   region_svalue (tree type, region_id rid) : svalue (type), m_rid (rid)
   {
     /* Should we support NULL ptrs here?  */
@@ -574,6 +577,9 @@ is_a_helper <region_svalue *>::test (svalue *sval)
 class constant_svalue : public svalue
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   constant_svalue (tree cst_expr)
   : svalue (TREE_TYPE (cst_expr)), m_cst_expr (cst_expr)
   {
@@ -631,6 +637,9 @@ is_a_helper <constant_svalue *>::test (svalue *sval)
 class unknown_svalue : public svalue
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   unknown_svalue (tree type)
   : svalue (type)
   {}
@@ -675,6 +684,9 @@ extern const char *poison_kind_to_str (enum poison_kind);
 class poisoned_svalue : public svalue
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   poisoned_svalue (enum poison_kind kind) : svalue (NULL), m_kind (kind) {}
 
   bool compare_fields (const poisoned_svalue &other) const;
@@ -916,6 +928,9 @@ public:
 class frame_region : public map_region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   frame_region (region_id parent_rid, function *fun, int depth)
   : map_region (parent_rid, NULL_TREE), m_fun (fun), m_depth (depth)
   {}
@@ -952,6 +967,9 @@ public:
 class stack_region : public region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   stack_region (region_id parent_rid, svalue_id sval_id)
   : region (parent_rid, sval_id, NULL_TREE)
   {}
@@ -1004,6 +1022,9 @@ public:
 class heap_region : public region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   heap_region (region_id parent_rid, svalue_id sval_id)
   : region (parent_rid, sval_id, NULL_TREE)
   {}
@@ -1031,6 +1052,9 @@ public:
 class root_region : public region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   root_region ();
   root_region (const root_region &other);
 
@@ -1092,6 +1116,9 @@ private:
 class symbolic_region : public map_region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   symbolic_region (region_id parent_rid)
   : map_region (parent_rid, NULL_TREE)
   {}
@@ -1116,6 +1143,9 @@ public:
 class region_model
 {
  public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   region_model ();
   region_model (const region_model &other);
   ~region_model ();
-- 
1.8.5.3

