public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] Implement visitor pattern for vrange.
@ 2022-07-15  9:40 Aldy Hernandez
  2022-07-15  9:40 ` [COMMITTED] Convert vrange dumping facilities to pretty_printer Aldy Hernandez
  2022-07-15  9:40 ` [COMMITTED] Use pp_vrange for ranges in dump_ssaname_info Aldy Hernandez
  0 siblings, 2 replies; 4+ messages in thread
From: Aldy Hernandez @ 2022-07-15  9:40 UTC (permalink / raw)
  To: GCC patches

We frequently do operations on the various (upcoming) range types.
The cascading if/switch statements of is_a<> are getting annoying and
repetitive.

The classic visitor pattern provides a clean way to implement classes
handling various range types without the need for endless
conditionals.  It also helps us keep polluting the vrange API with
functionality that should frankly live elsewhere.

In a follow-up patch I will add pretty printing facilities for vrange
and unify them with the dumping code.  This is a prime candidate for
the pattern, as the code isn't performance sensitive.  Other instances
(?? the dispatch code in range-ops ??) may still benefit from the hand
coded conditionals, since they elide vtables in favor of the
discriminator bit in vrange.

Tested on x86-64 Linux.

gcc/ChangeLog:

	* value-range.cc (irange::accept): New.
	(unsupported_range::accept): New.
	* value-range.h (class vrange_visitor): New.
	(class vrange): Add accept method.
	(class unsupported_range): Same.
	(class Value_Range): Same.
---
 gcc/value-range.cc | 12 ++++++++++++
 gcc/value-range.h  | 11 +++++++++++
 2 files changed, 23 insertions(+)

diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index 528ed547ef3..8e6ec4cd740 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -30,6 +30,18 @@ along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "gimple-range.h"
 
+void
+irange::accept (const vrange_visitor &v) const
+{
+  v.visit (*this);
+}
+
+void
+unsupported_range::accept (const vrange_visitor &v) const
+{
+  v.visit (*this);
+}
+
 // Convenience function only available for integers and pointers.
 
 wide_int
diff --git a/gcc/value-range.h b/gcc/value-range.h
index 0e341185f69..a7da8c5e900 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -73,6 +73,7 @@ class vrange
   template <typename T> friend bool is_a (vrange &);
   friend class Value_Range;
 public:
+  virtual void accept (const class vrange_visitor &v) const = 0;
   virtual void set (tree, tree, value_range_kind = VR_RANGE);
   virtual tree type () const;
   virtual bool supports_type_p (tree type) const;
@@ -149,6 +150,7 @@ public:
   // Misc methods.
   virtual bool fits_p (const vrange &r) const override;
   virtual void dump (FILE * = stderr) const override;
+  virtual void accept (const vrange_visitor &v) const override;
 
   // Nonzero masks.
   wide_int get_nonzero_bits () const;
@@ -251,6 +253,7 @@ class unsupported_range : public vrange
 public:
   unsupported_range ();
   virtual void dump (FILE *) const override;
+  virtual void accept (const vrange_visitor &v) const override;
 };
 
 // is_a<> and as_a<> implementation for vrange.
@@ -298,6 +301,13 @@ is_a <irange> (vrange &v)
   return v.m_discriminator == VR_IRANGE;
 }
 
+class vrange_visitor
+{
+public:
+  virtual void visit (const irange &) const { }
+  virtual void visit (const unsupported_range &) const { }
+};
+
 // This is a special int_range<1> with only one pair, plus
 // VR_ANTI_RANGE magic to describe slightly more than can be described
 // in one pair.  It is described in the code as a "legacy range" (as
@@ -348,6 +358,7 @@ public:
   bool zero_p () const { return m_vrange->zero_p (); }
   wide_int lower_bound () const; // For irange/prange compatability.
   wide_int upper_bound () const; // For irange/prange compatability.
+  void accept (const vrange_visitor &v) const { m_vrange->accept (v); }
 private:
   void init (tree type);
   unsupported_range m_unsupported;
-- 
2.36.1


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

end of thread, other threads:[~2022-07-15 12:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15  9:40 [COMMITTED] Implement visitor pattern for vrange Aldy Hernandez
2022-07-15  9:40 ` [COMMITTED] Convert vrange dumping facilities to pretty_printer Aldy Hernandez
2022-07-15  9:40 ` [COMMITTED] Use pp_vrange for ranges in dump_ssaname_info Aldy Hernandez
2022-07-15 12:30   ` Aldy Hernandez

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).