public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] irange_pool class
@ 2020-09-17 10:36 Aldy Hernandez
  2020-09-17 18:02 ` Martin Sebor
  2020-09-18  1:43 ` David Malcolm
  0 siblings, 2 replies; 18+ messages in thread
From: Aldy Hernandez @ 2020-09-17 10:36 UTC (permalink / raw)
  To: gcc-patches, Andrew MacLeod

This is the irange storage class.  It is used to allocate the minimum 
amount of storage needed for a given irange.  Storage is automatically 
freed at destruction.

It is meant for long term storage, as opposed to int_range_max which is 
meant for intermediate temporary results on the stack.

The general gist is:

	irange_pool pool;

	// Allocate an irange of 5 sub-ranges.
	irange *p = pool.allocate (5);

	// Allocate an irange of 3 sub-ranges.
	irange *q = pool.allocate (3);

	// Allocate an irange with as many sub-ranges as are currently
	// used in "some_other_range".
	irange *r = pool.allocate (some_other_range);

OK?
Aldy
---
  gcc/value-range.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 63 insertions(+)

diff --git a/gcc/value-range.h b/gcc/value-range.h
index 8497791c7b3..88cb3075bf0 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -43,6 +43,7 @@ enum value_range_kind

  class irange
  {
+  friend class irange_pool;
  public:
    // In-place setters.
    void set (tree, tree, value_range_kind = VR_RANGE);
@@ -619,4 +620,66 @@ vrp_val_min (const_tree type)
    return NULL_TREE;
  }

+// This is the irange storage class.  It is used to allocate the
+// minimum amount of storage needed for a given irange.  Storage is
+// automatically freed at destruction.
+//
+// It is meant for long term storage, as opposed to int_range_max
+// which is meant for intermediate temporary results on the stack.
+
+class irange_pool
+{
+public:
+  irange_pool ();
+  ~irange_pool ();
+  // Return a new range with NUM_PAIRS.
+  irange *allocate (unsigned num_pairs);
+  // Return a copy of SRC with the minimum amount of sub-ranges needed
+  // to represent it.
+  irange *allocate (const irange &src);
+private:
+  struct obstack irange_obstack;
+};
+
+inline
+irange_pool::irange_pool ()
+{
+  obstack_init (&irange_obstack);
+}
+
+inline
+irange_pool::~irange_pool ()
+{
+  obstack_free (&irange_obstack, NULL);
+}
+
+// Return a new range with NUM_PAIRS.
+
+inline irange *
+irange_pool::allocate (unsigned num_pairs)
+{
+  // Never allocate 0 pairs.
+  // Don't allocate 1 either, or we get legacy value_range's.
+  if (num_pairs < 2)
+    num_pairs = 2;
+
+  struct newir {
+    irange range;
+    tree mem[1];
+  };
+  struct newir *r
+    = (struct newir *) obstack_alloc (&irange_obstack,
+				      sizeof (struct newir)
+				      + sizeof (tree) * 2 * (num_pairs - 1));
+  return new ((irange *) r) irange (&(r->mem[0]), num_pairs);
+}
+
+inline irange *
+irange_pool::allocate (const irange &src)
+{
+  irange *r = allocate (src.num_pairs ());
+  *r = src;
+  return r;
+}
+
  #endif // GCC_VALUE_RANGE_H
-- 
2.26.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2020-09-28 15:56 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 10:36 [PATCH] irange_pool class Aldy Hernandez
2020-09-17 18:02 ` Martin Sebor
2020-09-18  6:17   ` Aldy Hernandez
2020-09-18  1:43 ` David Malcolm
2020-09-18  5:49   ` Aldy Hernandez
2020-09-18 12:28     ` David Malcolm
2020-09-18 14:10       ` Aldy Hernandez
2020-09-18 17:07         ` Martin Sebor
2020-09-18 17:36           ` Andrew MacLeod
2020-09-18 20:35             ` Martin Sebor
2020-09-18 21:09               ` Andrew MacLeod
2020-09-19 20:32                 ` Martin Sebor
2020-09-20  0:40                   ` Andrew MacLeod
2020-09-20  7:01                   ` Aldy Hernandez
2020-09-21 14:14                   ` Andrew MacLeod
2020-09-18 16:42       ` Andrew MacLeod
2020-09-18 17:03         ` Aldy Hernandez
2020-09-28 15:56           ` Andrew MacLeod

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).