diff mbox series

Add new/delete to struct occurence

Message ID nycvar.YFH.7.76.2006041623320.4397@zhemvz.fhfr.qr
State New
Headers show
Series Add new/delete to struct occurence | expand

Commit Message

Richard Biener June 4, 2020, 2:25 p.m. UTC
I'm testing the following patch from Jonathan (I asked for an
example how to retrofit an alloc-pool user to new/delete style
operation).  It also introduces NSDMI for member init.

Bootstrap / regtest ongoing on x86_64-unknown-linux-gnu with
a GCC 4.8.5 host compiler (just to make sure...)

I'll be doing the same to vectorizer SLP nodes if it works out.

Richard.

--

This adds an example how to use new/delete operators to pool
allocated objects.

2020-06-04  Jonathan Wakely  <jwakely@redhat.com>

	* alloc-pool.h (object_allocator::remove_raw): New.
	* tree-ssa-math-opts.c (struct occurrence): Use NSMDI.
	(occurrence::occurrence): Add.
	(occurrence::~occurrence): Likewise.
	(occurrence::new): Likewise.
	(occurrence::delete): Likewise.
	(occ_new): Remove.
	(insert_bb): Use new occurence (...) instead of occ_new.
	(register_division_in): Likewise.
	(free_bb): Use delete occ instead of manually removing
	from the pool.
---
 gcc/alloc-pool.h         |  6 ++++
 gcc/tree-ssa-math-opts.c | 65 ++++++++++++++++++++++++----------------
 2 files changed, 45 insertions(+), 26 deletions(-)
diff mbox series

Patch

diff --git a/gcc/alloc-pool.h b/gcc/alloc-pool.h
index fd7194bfea4..4e78dcf6178 100644
--- a/gcc/alloc-pool.h
+++ b/gcc/alloc-pool.h
@@ -524,6 +524,12 @@  public:
     m_allocator.remove (object);
   }
 
+  inline void
+  remove_raw (void *object)
+  {
+    m_allocator.remove (object);
+  }
+
   inline size_t
   num_elts_current ()
   {
diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c
index 5fbaa24142e..237eb641589 100644
--- a/gcc/tree-ssa-math-opts.c
+++ b/gcc/tree-ssa-math-opts.c
@@ -121,37 +121,57 @@  along with GCC; see the file COPYING3.  If not see
    division.  */
 struct occurrence {
   /* The basic block represented by this structure.  */
-  basic_block bb;
+  basic_block bb = basic_block();
 
   /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
      inserted in BB.  */
-  tree recip_def;
+  tree recip_def = tree();
 
   /* If non-NULL, the SSA_NAME holding the definition for a squared
      reciprocal inserted in BB.  */
-  tree square_recip_def;
+  tree square_recip_def = tree();
 
   /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
      was inserted in BB.  */
-  gimple *recip_def_stmt;
+  gimple *recip_def_stmt = nullptr;
 
   /* Pointer to a list of "struct occurrence"s for blocks dominated
      by BB.  */
-  struct occurrence *children;
+  struct occurrence *children = nullptr;
 
   /* Pointer to the next "struct occurrence"s in the list of blocks
      sharing a common dominator.  */
-  struct occurrence *next;
+  struct occurrence *next = nullptr;
 
   /* The number of divisions that are in BB before compute_merit.  The
      number of divisions that are in BB or post-dominate it after
      compute_merit.  */
-  int num_divisions;
+  int num_divisions = 0;
 
   /* True if the basic block has a division, false if it is a common
      dominator for basic blocks that do.  If it is false and trapping
      math is active, BB is not a candidate for inserting a reciprocal.  */
-  bool bb_has_division;
+  bool bb_has_division = false;
+
+  /* Construct a struct occurrence for basic block BB, and whose
+     children list is headed by CHILDREN.  */
+  occurrence (basic_block bb, struct occurrence *children)
+  : bb (bb), children (children)
+  {
+    bb->aux = this;
+  }
+
+  /* Destroy a struct occurrence and remove it from its basic block.  */
+  ~occurrence ()
+  {
+    bb->aux = nullptr;
+  }
+
+  /* Allocate memory for a struct occurrence from OCC_POOL.  */
+  static void* operator new (size_t);
+
+  /* Return memory for a struct occurrence to OCC_POOL.  */
+  static void operator delete (void*, size_t);
 };
 
 static struct
@@ -191,23 +211,17 @@  static struct occurrence *occ_head;
 /* Allocation pool for getting instances of "struct occurrence".  */
 static object_allocator<occurrence> *occ_pool;
 
-
-
-/* Allocate and return a new struct occurrence for basic block BB, and
-   whose children list is headed by CHILDREN.  */
-static struct occurrence *
-occ_new (basic_block bb, struct occurrence *children)
+void* occurrence::operator new (size_t n)
 {
-  struct occurrence *occ;
-
-  bb->aux = occ = occ_pool->allocate ();
-  memset (occ, 0, sizeof (struct occurrence));
-
-  occ->bb = bb;
-  occ->children = children;
-  return occ;
+  gcc_assert (n == sizeof(occurrence));
+  return occ_pool->allocate_raw ();
 }
 
+void occurrence::operator delete (void *occ, size_t n)
+{
+  gcc_assert (n == sizeof(occurrence));
+  occ_pool->remove_raw (occ);
+}
 
 /* Insert NEW_OCC into our subset of the dominator tree.  P_HEAD points to a
    list of "struct occurrence"s, one per basic block, having IDOM as
@@ -259,7 +273,7 @@  insert_bb (struct occurrence *new_occ, basic_block idom,
 	  /* None of the previous blocks has DOM as a dominator: if we tail
 	     recursed, we would reexamine them uselessly. Just switch BB with
 	     DOM, and go on looking for blocks dominated by DOM.  */
-          new_occ = occ_new (dom, new_occ);
+          new_occ = new occurrence (dom, new_occ);
 	}
 
       else
@@ -288,7 +302,7 @@  register_division_in (basic_block bb, int importance)
   occ = (struct occurrence *) bb->aux;
   if (!occ)
     {
-      occ = occ_new (bb, NULL);
+      occ = new occurrence (bb, NULL);
       insert_bb (occ, ENTRY_BLOCK_PTR_FOR_FN (cfun), &occ_head);
     }
 
@@ -518,8 +532,7 @@  free_bb (struct occurrence *occ)
   /* First get the two pointers hanging off OCC.  */
   next = occ->next;
   child = occ->children;
-  occ->bb->aux = NULL;
-  occ_pool->remove (occ);
+  delete occ;
 
   /* Now ensure that we don't recurse unless it is necessary.  */
   if (!child)