From patchwork Mon Jul 20 13:05:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1332374 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=Z2rhNiaH; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B9MQp08Dyz9sRW for ; Mon, 20 Jul 2020 23:05:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BCE7C3861865; Mon, 20 Jul 2020 13:05:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id BF7F23861854 for ; Mon, 20 Jul 2020 13:05:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BF7F23861854 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=roger@nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QWExQAjVt5lo7BSRoTeTGIBUb0UYc0cTQ5gCxrjBBU4=; b=Z2rhNiaHGjypVzB2cLQTbjKaTK T5VFk/AQ/c27pfuttJQ/IjnM8r7lLDUWH5goEMYCZ7UFJiqTPm7YdQ0TGSEg7KU+Y2q4t1RoFp1rn nyPaO4cWwtoBZPH9qlbDAVNVxSjuTbA42o9s/OS1fpw/6UXoQ4X3cXanTNHf8tvQWtrSaSL0bV7Jl 0NXopKjGQ9QmvBQ26Gd7/K11m5XEWzazg8pKiDl46h3l7LuL/S2QZT2N4CfMXCLDXwh7YROqhTse+ XxTRPmFqV1k5xqWBWozMcT68AzjOzzbwKqC8b1HwYhY31BgUgf7A+qPB+IjKK2dDdvVopLom0i5Mo 9dKJU2hw==; Received: from host86-137-89-56.range86-137.btcentralplus.com ([86.137.89.56]:57711 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1jxVTm-0006tF-0Q for gcc-patches@gcc.gnu.org; Mon, 20 Jul 2020 09:05:14 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] middle-end: Fold popcount(x&4) to (x>>2)&1 and friends. Date: Mon, 20 Jul 2020 14:05:12 +0100 Message-ID: <013901d65e96$68255230$386ff690$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdZelabR4uF4kyCTRRCj1dGUPki68w== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This patch complements one from June 12th which is still awaiting review: https://gcc.gnu.org/pipermail/gcc-patches/2020-June/547937.html This patch optimizes popcount and parity of an argument known to have at most a single bit set, to be that single bit. Hence, popcount(x&8) is simplified to (x>>3)&1. This generalizes the existing optimization of popcount(x&1) being simplified to x&1, which is moved with this patch to avoid a duplicate pattern warning in match.pd. This patch has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new failures. If this is approved after (or at the same time) as the patch above, I'm happy to resolve the conflicts and retest before committing. 2020-07-20 Roger Sayle gcc/ChangeLog * match.pd (popcount(x) -> x>>C): New simplification. gcc/testsuite * gcc.dg/fold-popcount-5.c: New test. * gcc.dg/fold-parity-5.c: Likewise. Ok for mainline? Thanks in advance, Roger --- Roger Sayle NextMove Software Cambridge, UK diff --git a/gcc/match.pd b/gcc/match.pd index c6ae7a7..0b3b626 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5966,11 +5966,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* POPCOUNT simplifications. */ (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL BUILT_IN_POPCOUNTIMAX) - /* popcount(X&1) is nop_expr(X&1). */ - (simplify - (popcount @0) - (if (tree_nonzero_bits (@0) == 1) - (convert @0))) /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ (simplify (plus (popcount:s @0) (popcount:s @1)) @@ -5983,6 +5978,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp (popcount @0) integer_zerop) (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) +/* popcount(X&C1) is (X>>C2)&1 when C1 == 1<