From patchwork Mon Sep 26 17:24:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1472 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp1606139wrt; Mon, 26 Sep 2022 10:25:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Po5gbViEAzogFiWJTpwaVFpRnbsXc0+nPrgZna4TqprufvKS+PaE+j0fkF/7B/9ZnrOwx X-Received: by 2002:a05:6402:50d3:b0:451:42e0:a0de with SMTP id h19-20020a05640250d300b0045142e0a0demr24308627edb.185.1664213136669; Mon, 26 Sep 2022 10:25:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664213136; cv=none; d=google.com; s=arc-20160816; b=IdZua76bU179pZjfqfzWHDH0U+fehdJZGcpl5Dz33wIBM2p7WoS6V5UQ5Jo4IpPD50 smYKoKFSJB6PcOkGG3q2qVFOrr3s7CcHvfN059bxU4lIy0Jzi9Rs25fcPl5+PdLfwkSc oNb+kPj7+RXfIGcq23I7/p90tz293zlNVxgWo30MvUGyxN9w/x2sv/DfvErTSHB8zrYl yea50V6ONXE6lMdxCEv4sxbxjO0k9RUs2LSl4e78qC+ZT2iGvNd+w1iNArKp3t1lwYCV +FhaRTa52LAFuISskdEYQ8faBEum8G/eTOYy/yk2SpjCkRoYHIFeMK5wBw5lVGfwU8G6 lw5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=FSdMGyNqojanvG4K38b10gQ4xk2xJqYVUlhlLbFwsJU=; b=rY3TLmauXiyWeQtjp8UtWotXnpu0BFRhKbSoxdtNowXWv7qDGbPo78LR1Q5pHQlvdt VU416y/aERQUJDpP9e9EmRrmRRiFquw5wjlfmlxGnBoMHABQzN10OoKr+dvH4wQb8Rs0 lye+/kedildXkXp3x6GxdPsNbN+3wSiN7Ttmw7CXPO4hEC6F8OXq0GLBiBvT5ENvf6IZ wpBWJXZMaboeRh01m/ntoXjlZ/IK/5KC8cunBBGYIskxXqxE76E2/8BqudC2Ih/YRYHD GQCJ7tYTO0goaFQVhAyqVarmo5a3eHg+du0BFD3l7QFgFXpdbxwCX/MfW9LEe3RISp0O dHQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RVCV3hYj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i9-20020a1709064fc900b007805f59299dsi552679ejw.291.2022.09.26.10.25.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Sep 2022 10:25:36 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RVCV3hYj; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7AD2F385829B for ; Mon, 26 Sep 2022 17:25:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7AD2F385829B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664213135; bh=FSdMGyNqojanvG4K38b10gQ4xk2xJqYVUlhlLbFwsJU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RVCV3hYjSpuULhrKKH0Irx+rZDCRfmpdYVr+qDrbaIsvJPpQzmdnSokbOjvLGyrXg KTj6fGGcpnGUj8xW5PfLbKysTMeG4PtC6I7YGOEGBYptf8XgC/hycR4BPZneKJsIps ZtPW1JC/GzhegjlDpYI0uWjmQtrMCw7LgRss+f6s= 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 9AFE538582BF for ; Mon, 26 Sep 2022 17:24:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9AFE538582BF 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-13-bLJjZqeJMjihm8iB-ngX4w-1; Mon, 26 Sep 2022 13:24:50 -0400 X-MC-Unique: bLJjZqeJMjihm8iB-ngX4w-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 504373817964 for ; Mon, 26 Sep 2022 17:24:50 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.195.119]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E1FDF492B05; Mon, 26 Sep 2022 17:24:49 +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 28QHOltD3219498 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 26 Sep 2022 19:24:47 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 28QHOln73219497; Mon, 26 Sep 2022 19:24:47 +0200 To: GCC patches Subject: [COMMITTED] Optimize [0 = x & MASK] in range-ops. Date: Mon, 26 Sep 2022 19:24:41 +0200 Message-Id: <20220926172441.3219466-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745053953743557266?= X-GMAIL-MSGID: =?utf-8?q?1745053953743557266?= For [0 = x & MASK], we can determine that x is ~MASK. This is something we're picking up in DOM thanks to maybe_set_nonzero_bits, but is something we should handle natively. This is a good example of how much easier to maintain the range-ops entries are versus the ad-hoc pattern matching stuff we had to do before. For the curious, compare the changes to range-op here, versus maybe_set_nonzero_bits. I'm leaving the call to maybe_set_nonzero_bits until I can properly audit it to make sure we're catching it all in range-ops. It won't hurt, since both set_range_info() and set_nonzero_bits() are intersect operations, so we'll never lose information if we do both. Tested on x86-64 Linux. PR tree-optimization/107009 gcc/ChangeLog: * range-op.cc (operator_bitwise_and::op1_range): Optimize 0 = x & MASK. (range_op_bitwise_and_tests): New test. --- gcc/range-op.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 072ebd32109..fc930f4d613 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -2951,6 +2951,15 @@ operator_bitwise_and::op1_range (irange &r, tree type, } if (r.undefined_p ()) set_nonzero_range_from_mask (r, type, lhs); + + // For 0 = op1 & MASK, op1 is ~MASK. + if (lhs.zero_p () && op2.singleton_p ()) + { + wide_int nz = wi::bit_not (op2.get_nonzero_bits ()); + int_range<2> tmp (type); + tmp.set_nonzero_bits (nz); + r.intersect (tmp); + } return true; } @@ -4612,6 +4621,15 @@ range_op_bitwise_and_tests () op_bitwise_and.op1_range (res, integer_type_node, i1, i2); ASSERT_TRUE (res == int_range<1> (integer_type_node)); + // For 0 = x & MASK, x is ~MASK. + { + int_range<2> zero (integer_zero_node, integer_zero_node); + int_range<2> mask = int_range<2> (INT (7), INT (7)); + op_bitwise_and.op1_range (res, integer_type_node, zero, mask); + wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node)); + ASSERT_TRUE (res.get_nonzero_bits () == inv); + } + // (NONZERO | X) is nonzero. i1.set_nonzero (integer_type_node); i2.set_varying (integer_type_node);