From patchwork Mon May 1 06:29:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1775416 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=BYWMxrjk; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q8td8728yz1ydX for ; Mon, 1 May 2023 16:30:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0198B384F024 for ; Mon, 1 May 2023 06:30:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0198B384F024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682922651; bh=WtHywSJRNS4T4nq0/3xXPf11hVlqtMBK/7l8Xt3Arek=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=BYWMxrjkILMdYeULma7KdKg2C2ZQ8KWUWePQJxBBNiyP64G0qSleUFwuQEo5bk+jd MfOkiGkDoFBgdq9RrbDQLfOn0cc5gjsTS1IPE4ynqYWtqymo2COqCi5TKXVAJPAKm7 2sCfrgeUM0vvoPbLcDDe3IqEzcx7zQtZ0+hE2wYI= 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 7BA493858425 for ; Mon, 1 May 2023 06:29:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BA493858425 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-374-W9rQ8T4ENLGmHHznC51COg-1; Mon, 01 May 2023 02:29:14 -0400 X-MC-Unique: W9rQ8T4ENLGmHHznC51COg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 332EC3801FF9 for ; Mon, 1 May 2023 06:29:14 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.81]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E264714171B6; Mon, 1 May 2023 06:29:13 +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 3416TC94564881 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 1 May 2023 08:29:12 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 3416TCu6564880; Mon, 1 May 2023 08:29:12 +0200 To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] Cleanup irange::set. Date: Mon, 1 May 2023 08:29:05 +0200 Message-Id: <20230501062906.564803-11-aldyh@redhat.com> In-Reply-To: <20230501062906.564803-1-aldyh@redhat.com> References: <20230501062906.564803-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Now that anti-ranges are no more and iranges contain wide_ints instead of trees, various cleanups are possible. This is one of a handful of patches improving the performance of irange::set() which is not on a hot path, but quite sensitive because it is so pervasive. gcc/ChangeLog: * gimple-range-op.cc (cfn_ffs::fold_range): Use the correct precision. * gimple-ssa-warn-alloca.cc (alloca_call_type): Use <2> for invalid_range, as it is an inverse range. * tree-vrp.cc (find_case_label_range): Avoid trees. * value-range.cc (irange::irange_set): Delete. (irange::irange_set_1bit_anti_range): Delete. (irange::irange_set_anti_range): Delete. (irange::set): Cleanup. * value-range.h (class irange): Remove irange_set, irange_set_anti_range, irange_set_1bit_anti_range. (irange::set_undefined): Remove set to m_type. --- gcc/gimple-range-op.cc | 4 +- gcc/gimple-ssa-warn-alloca.cc | 2 +- gcc/tree-vrp.cc | 8 +- gcc/value-range.cc | 175 ++++++++++------------------------ gcc/value-range.h | 5 - 5 files changed, 59 insertions(+), 135 deletions(-) diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index 3aef8357d8d..5d1f921ba40 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -654,7 +654,9 @@ public: range_cast (tmp, unsigned_type_for (tmp.type ())); wide_int max = tmp.upper_bound (); maxi = wi::floor_log2 (max) + 1; - r.set (type, wi::shwi (mini, prec), wi::shwi (maxi, prec)); + r.set (type, + wi::shwi (mini, TYPE_PRECISION (type)), + wi::shwi (maxi, TYPE_PRECISION (type))); return true; } } op_cfn_ffs; diff --git a/gcc/gimple-ssa-warn-alloca.cc b/gcc/gimple-ssa-warn-alloca.cc index c129aca16e2..2d8ab93a81d 100644 --- a/gcc/gimple-ssa-warn-alloca.cc +++ b/gcc/gimple-ssa-warn-alloca.cc @@ -222,7 +222,7 @@ alloca_call_type (gimple *stmt, bool is_vla) && !r.varying_p ()) { // The invalid bits are anything outside of [0, MAX_SIZE]. - int_range<1> invalid_range (size_type_node, + int_range<2> invalid_range (size_type_node, wi::shwi (0, TYPE_PRECISION (size_type_node)), wi::shwi (max_size, TYPE_PRECISION (size_type_node)), VR_ANTI_RANGE); diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc index d28637b1918..0761b6896fe 100644 --- a/gcc/tree-vrp.cc +++ b/gcc/tree-vrp.cc @@ -827,6 +827,8 @@ find_case_label_range (gswitch *switch_stmt, const irange *range_of_op) size_t i, j; tree op = gimple_switch_index (switch_stmt); tree type = TREE_TYPE (op); + unsigned prec = TYPE_PRECISION (type); + signop sign = TYPE_SIGN (type); tree tmin = wide_int_to_tree (type, range_of_op->lower_bound ()); tree tmax = wide_int_to_tree (type, range_of_op->upper_bound ()); find_case_label_range (switch_stmt, tmin, tmax, &i, &j); @@ -837,9 +839,11 @@ find_case_label_range (gswitch *switch_stmt, const irange *range_of_op) tree label = gimple_switch_label (switch_stmt, i); tree case_high = CASE_HIGH (label) ? CASE_HIGH (label) : CASE_LOW (label); + wide_int wlow = wi::to_wide (CASE_LOW (label)); + wide_int whigh = wi::to_wide (case_high); int_range_max label_range (type, - wi::to_wide (CASE_LOW (label)), - wi::to_wide (case_high)); + wide_int::from (wlow, prec, sign), + wide_int::from (whigh, prec, sign)); if (!types_compatible_p (label_range.type (), range_of_op->type ())) range_cast (label_range, range_of_op->type ()); label_range.intersect (*range_of_op); diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 2dc6b98bc63..655ffc2d6d4 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -961,98 +961,6 @@ get_legacy_range (const irange &r, tree &min, tree &max) return VR_RANGE; } -void -irange::irange_set (tree type, const wide_int &min, const wide_int &max) -{ - m_type = type; - m_base[0] = min; - m_base[1] = max; - m_num_ranges = 1; - m_kind = VR_RANGE; - m_nonzero_mask = wi::minus_one (TYPE_PRECISION (type)); - normalize_kind (); - - if (flag_checking) - verify_range (); -} - -void -irange::irange_set_1bit_anti_range (tree type, - const wide_int &min, const wide_int &max) -{ - unsigned prec = TYPE_PRECISION (type); - signop sign = TYPE_SIGN (type); - gcc_checking_assert (prec == 1); - - if (min == max) - { - wide_int tmp; - // Since these are 1-bit quantities, they can only be [MIN,MIN] - // or [MAX,MAX]. - if (min == wi::min_value (prec, sign)) - tmp = wi::max_value (prec, sign); - else - tmp = wi::min_value (prec, sign); - set (type, tmp, tmp); - } - else - { - // The only alternative is [MIN,MAX], which is the empty range. - gcc_checking_assert (min == wi::min_value (prec, sign)); - gcc_checking_assert (max == wi::max_value (prec, sign)); - set_undefined (); - } - if (flag_checking) - verify_range (); -} - -void -irange::irange_set_anti_range (tree type, - const wide_int &min, const wide_int &max) -{ - if (TYPE_PRECISION (type) == 1) - { - irange_set_1bit_anti_range (type, min, max); - return; - } - - // set an anti-range - signop sign = TYPE_SIGN (type); - int_range<2> type_range (type); - // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX]. - m_num_ranges = 0; - wi::overflow_type ovf; - - if (wi::ne_p (min, type_range.lower_bound ())) - { - wide_int lim1 = wi::sub (min, 1, sign, &ovf); - gcc_checking_assert (ovf != wi::OVF_OVERFLOW); - m_base[0] = type_range.lower_bound (0); - m_base[1] = lim1; - m_num_ranges = 1; - } - if (wi::ne_p (max, type_range.upper_bound ())) - { - if (m_max_ranges == 1 && m_num_ranges) - { - set_varying (type); - return; - } - wide_int lim2 = wi::add (max, 1, sign, &ovf); - gcc_checking_assert (ovf != wi::OVF_OVERFLOW); - m_base[m_num_ranges * 2] = lim2; - m_base[m_num_ranges * 2 + 1] = type_range.upper_bound (0); - ++m_num_ranges; - } - - m_kind = VR_RANGE; - m_nonzero_mask = wi::minus_one (TYPE_PRECISION (type)); - normalize_kind (); - - if (flag_checking) - verify_range (); -} - /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}. This means adjusting VRTYPE, MIN and MAX representing the case of a wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX] @@ -1063,48 +971,69 @@ irange::irange_set_anti_range (tree type, extract ranges from var + CST op limit. */ void -irange::set (tree type, const wide_int &rmin, const wide_int &rmax, +irange::set (tree type, const wide_int &min, const wide_int &max, value_range_kind kind) { - if (kind == VR_UNDEFINED) - { - irange::set_undefined (); - return; - } - - if (kind == VR_VARYING) - { - set_varying (type); - return; - } + unsigned prec = TYPE_PRECISION (type); + signop sign = TYPE_SIGN (type); + wide_int min_value = wi::min_value (prec, sign); + wide_int max_value = wi::max_value (prec, sign); m_type = type; - signop sign = TYPE_SIGN (type); - unsigned prec = TYPE_PRECISION (type); - wide_int min = wide_int::from (rmin, prec, sign); - wide_int max = wide_int::from (rmax, prec, sign); + m_nonzero_mask = wi::minus_one (prec); if (kind == VR_RANGE) - irange_set (type, min, max); + { + m_base[0] = min; + m_base[1] = max; + m_num_ranges = 1; + if (min == min_value && max == max_value) + m_kind = VR_VARYING; + else + m_kind = VR_RANGE; + } else { gcc_checking_assert (kind == VR_ANTI_RANGE); - irange_set_anti_range (type, min, max); + gcc_checking_assert (m_max_ranges > 1); + + m_kind = VR_UNDEFINED; + m_num_ranges = 0; + wi::overflow_type ovf; + wide_int lim; + if (sign == SIGNED) + lim = wi::add (min, -1, sign, &ovf); + else + lim = wi::sub (min, 1, sign, &ovf); + + if (!ovf) + { + m_kind = VR_RANGE; + m_base[0] = min_value; + m_base[1] = lim; + ++m_num_ranges; + } + if (sign == SIGNED) + lim = wi::sub (max, -1, sign, &ovf); + else + lim = wi::add (max, 1, sign, &ovf); + if (!ovf) + { + m_kind = VR_RANGE; + m_base[m_num_ranges * 2] = lim; + m_base[m_num_ranges * 2 + 1] = max_value; + ++m_num_ranges; + } } + + if (flag_checking) + verify_range (); } void irange::set (tree min, tree max, value_range_kind kind) { - if (kind == VR_UNDEFINED) - { - irange::set_undefined (); - return; - } - - if (kind == VR_VARYING - || POLY_INT_CST_P (min) - || POLY_INT_CST_P (max)) + if (POLY_INT_CST_P (min) || POLY_INT_CST_P (max)) { set_varying (TREE_TYPE (min)); return; @@ -1113,13 +1042,7 @@ irange::set (tree min, tree max, value_range_kind kind) gcc_checking_assert (TREE_CODE (min) == INTEGER_CST); gcc_checking_assert (TREE_CODE (max) == INTEGER_CST); - if (TREE_OVERFLOW_P (min)) - min = drop_tree_overflow (min); - if (TREE_OVERFLOW_P (max)) - max = drop_tree_overflow (max); - - return set (TREE_TYPE (min), - wi::to_wide (min), wi::to_wide (max), kind); + return set (TREE_TYPE (min), wi::to_wide (min), wi::to_wide (max), kind); } // Check the validity of the range. diff --git a/gcc/value-range.h b/gcc/value-range.h index 9f82b0011c7..9a834c91b17 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -172,8 +172,6 @@ protected: irange (wide_int *, unsigned); // In-place operators. - void irange_set (tree type, const wide_int &, const wide_int &); - void irange_set_anti_range (tree type, const wide_int &, const wide_int &); bool irange_contains_p (const irange &) const; bool irange_single_pair_union (const irange &r); @@ -186,8 +184,6 @@ private: friend void gt_pch_nx (irange *); friend void gt_pch_nx (irange *, gt_pointer_operator, void *); - void irange_set_1bit_anti_range (tree type, - const wide_int &, const wide_int &); bool varying_compatible_p () const; bool intersect_nonzero_bits (const irange &r); bool union_nonzero_bits (const irange &r); @@ -831,7 +827,6 @@ inline void irange::set_undefined () { m_kind = VR_UNDEFINED; - m_type = NULL; m_num_ranges = 0; }