From 0ae5c54c9040630fc5cded22b1dbeab3edbd8eda Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Tue, 8 Oct 2019 08:52:59 -0400
Subject: [PATCH 88/95] 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 ace8924..4934346 100644
--- a/gcc/analyzer/analyzer.h
+++ b/gcc/analyzer/analyzer.h
@@ -123,4 +123,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 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 197f1be..bb073a7 100644
--- a/gcc/analyzer/region-model.cc
+++ b/gcc/analyzer/region-model.cc
@@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "convert.h"
 #include "target.h"
 #include "selftest.h"
+#include "alloc-pool.h"
 #include "diagnostic-color.h"
 #include "diagnostic-metadata.h"
 #include "analyzer/analyzer.h"
@@ -379,6 +380,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==.  */
@@ -593,6 +598,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==.  */
@@ -696,6 +703,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==.  */
@@ -747,6 +756,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==.  */
@@ -2001,6 +2012,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 *
@@ -2021,6 +2034,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 *
@@ -2452,6 +2467,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)
@@ -2692,6 +2709,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)
@@ -2729,6 +2748,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 ()
@@ -3145,6 +3166,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)
@@ -3184,6 +3207,8 @@ symbolic_region::walk_for_canonicalization (canonicalization *) 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 6f92e94..772b0fa 100644
--- a/gcc/analyzer/region-model.h
+++ b/gcc/analyzer/region-model.h
@@ -521,6 +521,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?  */
@@ -581,6 +584,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)
   {
@@ -640,6 +646,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)
   {}
@@ -684,6 +693,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) {}
 
@@ -1032,6 +1044,9 @@ is_a_helper <struct_or_union_region *>::test (region *reg)
 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)
   {
@@ -1059,6 +1074,9 @@ is_a_helper <struct_region *>::test (region *reg)
 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)
   {
@@ -1359,6 +1377,9 @@ is_a_helper <array_region *>::test (region *reg)
 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)
   {}
@@ -1420,6 +1441,9 @@ is_a_helper <stack_region *>::test (region *reg)
 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)
   {}
@@ -1455,6 +1479,9 @@ is_a_helper <heap_region *>::test (region *reg)
 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);
 
@@ -1530,6 +1557,9 @@ is_a_helper <root_region *>::test (region *reg)
 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)
@@ -1562,6 +1592,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

