From patchwork Fri Jul 15 09:40:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 56092 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 90BB3384D1AC for ; Fri, 15 Jul 2022 09:41:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 90BB3384D1AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1657878083; bh=lRDKClM3Tn7jL3pK+pj0LQ1VAPyMclDw8CgLk0VIAfY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=yy2i9O6m8OHJOlgERMPhDnVCJ0arUwfan0FJLSL1OqgYVSa9t8DzbBTPWsmg7JY5K QJlCJWd7z9x6IDn9zJHwr6mfEnAIjuUD4XIcbIDuM5Bcpu077k3TwS36SOF2Hz3tjY 1uohcsDvVZvPbR8SWm4ydBE8yZOp3bUDEjM8LlZI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1230B385736C for ; Fri, 15 Jul 2022 09:40:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1230B385736C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-646-Y894BmMlMciLmXmhP_Aa4g-1; Fri, 15 Jul 2022 05:40:40 -0400 X-MC-Unique: Y894BmMlMciLmXmhP_Aa4g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFB1F101A588 for ; Fri, 15 Jul 2022 09:40:39 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.193.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6D1A3400DFC9; Fri, 15 Jul 2022 09:40:39 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 26F9eaMc1011300 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 15 Jul 2022 11:40:36 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 26F9eawN1011299; Fri, 15 Jul 2022 11:40:36 +0200 To: GCC patches Subject: [COMMITTED] Implement visitor pattern for vrange. Date: Fri, 15 Jul 2022 11:40:33 +0200 Message-Id: <20220715094035.1011279-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "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 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 (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;