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

---
 gcc/analyzer/analyzer.h            | 22 ++++++++++++++++++++
 gcc/analyzer/constraint-manager.cc |  3 +++
 gcc/analyzer/constraint-manager.h  |  3 +++
 gcc/analyzer/region-model.cc       | 25 ++++++++++++++++++++++
 gcc/analyzer/region-model.h        | 33 ++++++++++++++++++++++++++++++
 5 files changed, 86 insertions(+)

diff --git a/gcc/analyzer/analyzer.h b/gcc/analyzer/analyzer.h
index f7cfe9f5e7a..cc1abef8f07 100644
--- a/gcc/analyzer/analyzer.h
+++ b/gcc/analyzer/analyzer.h
@@ -114,4 +114,26 @@ struct pod_hash_traits : typed_noop_remove<Type>
   static inline bool is_empty (Type);
 };
 
+////////////////////////////////////////////////////////////////////////////
+
+/* 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 6fafef45bf3..fe5969d12f7 100644
--- a/gcc/analyzer/constraint-manager.cc
+++ b/gcc/analyzer/constraint-manager.cc
@@ -27,6 +27,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple.h"
 #include "gimple-iterator.h"
 #include "fold-const.h"
+#include "alloc-pool.h"
 #include "selftest.h"
 #include "diagnostic-core.h"
 #include "graphviz.h"
@@ -160,6 +161,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 9c8ef267c27..e2ebc967073 100644
--- a/gcc/analyzer/constraint-manager.h
+++ b/gcc/analyzer/constraint-manager.h
@@ -44,6 +44,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 25d71dd7ad5..2ec0d7c985d 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -36,6 +36,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "target.h"
 #include "fold-const.h"
 #include "tree-pretty-print.h"
+#include "alloc-pool.h"
 #include "diagnostic-color.h"
 #include "diagnostic-metadata.h"
 #include "tristate.h"
@@ -397,6 +398,10 @@ svalue::maybe_get_constant () 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==.  */
@@ -631,6 +636,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==.  */
@@ -736,6 +743,8 @@ constant_svalue::get_child_sid (region *parent ATTRIBUTE_UNUSED,
 
 /* 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==.  */
@@ -787,6 +796,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==.  */
@@ -2033,6 +2044,8 @@ struct_or_union_region::compare_fields (const struct_or_union_region &other)
 
 /* class struct_region : public struct_or_union_region.  */
 
+IMPL_POOL_ALLOCATOR (struct_region)
+
 /* Implementation of region::clone vfunc for struct_region.  */
 
 region *
@@ -2053,6 +2066,8 @@ struct_region::compare_fields (const struct_region &other) const
 
 /* class union_region : public struct_or_union_region.  */
 
+IMPL_POOL_ALLOCATOR (union_region)
+
 /* Implementation of region::clone vfunc for union_region.  */
 
 region *
@@ -2486,6 +2501,8 @@ function_region::valid_key_p (tree key) const
 
 /* class stack_region : public region.  */
 
+IMPL_POOL_ALLOCATOR (stack_region)
+
 /* stack_region's copy ctor.  */
 
 stack_region::stack_region (const stack_region &other)
@@ -2728,6 +2745,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)
@@ -2756,6 +2775,8 @@ heap_region::clone () const
 
 /* class root_region : public region.  */
 
+IMPL_POOL_ALLOCATOR (root_region)
+
 /* root_region's default ctor.  */
 
 root_region::root_region ()
@@ -3173,6 +3194,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)
@@ -3201,6 +3224,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 af410439de5..098fd6b4754 100644
--- a/gcc/analyzer/region-model.h
+++ b/gcc/analyzer/region-model.h
@@ -510,6 +510,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 @@ namespace ana {
 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)
   {
@@ -637,6 +643,9 @@ namespace ana {
 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)
   {}
@@ -681,6 +690,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, tree type)
   : svalue (type), m_kind (kind) {}
 
@@ -1064,6 +1076,9 @@ namespace ana {
 class struct_region : public struct_or_union_region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   struct_region (region_id parent_rid, tree type)
   : struct_or_union_region (parent_rid, type)
   {
@@ -1095,6 +1110,9 @@ namespace ana {
 class union_region : public struct_or_union_region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   union_region (region_id parent_rid, tree type)
   : struct_or_union_region (parent_rid, type)
   {
@@ -1421,6 +1439,9 @@ namespace ana {
 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)
   {}
@@ -1486,6 +1507,9 @@ namespace ana {
 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)
   {}
@@ -1522,6 +1546,9 @@ namespace ana {
 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);
 
@@ -1601,6 +1628,9 @@ namespace ana {
 class symbolic_region : public region
 {
 public:
+  static void *operator new (size_t sz);
+  static void operator delete (void* ptr, size_t sz);
+
   symbolic_region (region_id parent_rid, bool possibly_null)
   : region (parent_rid, svalue_id::null (), NULL_TREE),
     m_possibly_null (possibly_null)
@@ -1629,6 +1659,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 ();
-- 
2.21.0

