From patchwork Tue Jul 12 14:41:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1655459 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=nxrv0mnG; dkim-atps=neutral Authentication-Results: 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=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Lj3PH45Qpz9s07 for ; Wed, 13 Jul 2022 00:42:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E840F385AC3F for ; Tue, 12 Jul 2022 14:42:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E840F385AC3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1657636927; bh=apWLrs83XI1R96QsrQZqAspM0sAwX2R0CrKCuIW8cnY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=nxrv0mnGBwhQiOkHoc46pgmNLnLgw6TrWrOokawHGsYkoLTxhJv029Ah7WImOybp9 TT3OLh55vm6BODrwqVyvzWR+UUAmuR5WW4z+btawjPD9teNQ7ux2kBbZtsTBx4/05A U4uR04MjXOk9a/TDruORaTJYK5kG/kNU/wuuleGM= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 2FF833857C7D for ; Tue, 12 Jul 2022 14:41:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2FF833857C7D 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-641-RmIPuXZ3PP6fc7229rDTVw-1; Tue, 12 Jul 2022 10:41:45 -0400 X-MC-Unique: RmIPuXZ3PP6fc7229rDTVw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1DDE83C10688 for ; Tue, 12 Jul 2022 14:41:45 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C93CA40E8B04; Tue, 12 Jul 2022 14:41:44 +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 26CEfgup510754 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 12 Jul 2022 16:41:42 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 26CEfg5P510753; Tue, 12 Jul 2022 16:41:42 +0200 To: GCC patches Subject: [COMMITTED] Set nonzero bits from bitwise and operator in range-ops. Date: Tue, 12 Jul 2022 16:41:29 +0200 Message-Id: <20220712144129.510736-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_LOW, 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 nonzero bits are first class citizens in the range, we can keep better track of them in range-ops, especially the bitwise and operator. This patch sets the nonzero mask for the trivial case. In doing so, I've removed some old dead code that was an attempt to keep better track of masks. I'm sure there are tons of optimizations throughout range-ops that could be implemented, especially the op1_range methods, but those always make my head hurt. I'll leave them to the smarter hackers out there. I've removed the restriction that nonzero bits can't be queried from legacy. This was causing special casing all over the place, and it's not like we can generate incorrect code. We just silently drop nonzero bits to -1 in some of the legacy code. The end result is that VRP1, and other users of legacy, may not benefit from these improvements. Tested and benchmarked on x86-64 Linux. gcc/ChangeLog: * range-op.cc (unsigned_singleton_p): Remove. (operator_bitwise_and::remove_impossible_ranges): Remove. (operator_bitwise_and::fold_range): Set nonzero bits. * * value-range.cc (irange::get_nonzero_bits): Remove legacy_mode_p assert. (irange::dump_bitmasks): Remove legacy_mode_p check. --- gcc/range-op.cc | 70 ++-------------------------------------------- gcc/value-range.cc | 6 +--- 2 files changed, 4 insertions(+), 72 deletions(-) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 5150c6021b8..0e16408027c 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -2604,72 +2604,8 @@ private: void simple_op1_range_solver (irange &r, tree type, const irange &lhs, const irange &op2) const; - void remove_impossible_ranges (irange &r, const irange &rh) const; } op_bitwise_and; -static bool -unsigned_singleton_p (const irange &op) -{ - tree mask; - if (op.singleton_p (&mask)) - { - wide_int x = wi::to_wide (mask); - return wi::ge_p (x, 0, TYPE_SIGN (op.type ())); - } - return false; -} - -// Remove any ranges from R that are known to be impossible when an -// range is ANDed with MASK. - -void -operator_bitwise_and::remove_impossible_ranges (irange &r, - const irange &rmask) const -{ - if (r.undefined_p () || !unsigned_singleton_p (rmask)) - return; - - wide_int mask = rmask.lower_bound (); - tree type = r.type (); - int prec = TYPE_PRECISION (type); - int leading_zeros = wi::clz (mask); - int_range_max impossible_ranges; - - /* We know that starting at the most significant bit, any 0 in the - mask means the resulting range cannot contain a 1 in that same - position. This means the following ranges are impossible: - - x & 0b1001 1010 - IMPOSSIBLE RANGES - 01xx xxxx [0100 0000, 0111 1111] - 001x xxxx [0010 0000, 0011 1111] - 0000 01xx [0000 0100, 0000 0111] - 0000 0001 [0000 0001, 0000 0001] - */ - wide_int one = wi::one (prec); - for (int i = 0; i < prec - leading_zeros - 1; ++i) - if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0) - { - tree lb = fold_build2 (LSHIFT_EXPR, type, - build_one_cst (type), - build_int_cst (type, i)); - tree ub_left = fold_build1 (BIT_NOT_EXPR, type, - fold_build2 (LSHIFT_EXPR, type, - build_minus_one_cst (type), - build_int_cst (type, i))); - tree ub_right = fold_build2 (LSHIFT_EXPR, type, - build_one_cst (type), - build_int_cst (type, i)); - tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right); - impossible_ranges.union_ (int_range<1> (lb, ub)); - } - if (!impossible_ranges.undefined_p ()) - { - impossible_ranges.invert (); - r.intersect (impossible_ranges); - } -} - bool operator_bitwise_and::fold_range (irange &r, tree type, const irange &lh, @@ -2678,9 +2614,9 @@ operator_bitwise_and::fold_range (irange &r, tree type, { if (range_operator::fold_range (r, type, lh, rh)) { - // FIXME: This is temporarily disabled because, though it - // generates better ranges, it's noticeably slower for evrp. - // remove_impossible_ranges (r, rh); + if (!lh.undefined_p () && !rh.undefined_p ()) + r.set_nonzero_bits (wi::bit_and (lh.get_nonzero_bits (), + rh.get_nonzero_bits ())); return true; } return false; diff --git a/gcc/value-range.cc b/gcc/value-range.cc index a02fab47fc4..2aa973b2af2 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -2388,10 +2388,6 @@ wide_int irange::get_nonzero_bits () const { gcc_checking_assert (!undefined_p ()); - // Nonzero bits are unsupported in legacy mode. The mask may be set - // as a consequence of propagation or reading global ranges, but no - // one from legacy land should be querying this. - gcc_checking_assert (!legacy_mode_p ()); // Calculate the nonzero bits inherent in the range. wide_int min = lower_bound (); @@ -2509,7 +2505,7 @@ irange::dump (FILE *file) const void irange::dump_bitmasks (FILE *file) const { - if (m_nonzero_mask && !legacy_mode_p ()) + if (m_nonzero_mask) { wide_int nz = get_nonzero_bits (); if (nz != -1)