From patchwork Tue Jul 28 22:08:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1338030 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=8.43.85.97; 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=fxbFF8sn; dkim-atps=neutral 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 (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BGW5m5m4nz9sRN for ; Wed, 29 Jul 2020 08:08:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F8763857C67; Tue, 28 Jul 2020 22:08:26 +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 305EE3858D35 for ; Tue, 28 Jul 2020 22:08:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 305EE3858D35 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:Cc:To:From:Sender:Reply-To: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=vcxY0ayrIN7SPFbhIQfBY5ym55qA+2vPSqEbK4LFIjE=; b=fxbFF8sn8FYtw76axuA28GDwRL 5LPtea+a1hmfDLsv8W/XNTJuNd9/FUgH2PIg4LPcosRkmL+ODIs07aJSlLzi8XD3YfIYd9GjYqBhq voYsaifV+keNPA1PuDDdvvtrwtbyQvfVURG99MOyZp0owNnIs3OK6qP73OA+WNzH4b+Sgd0LECRHR oiPuk6DOy7ZvHzwae7Dp9Td00jIyF3KKGylda9WbWP8/Z1PJp0+Z+DRoz1BIda6clYkhMwAcrlq5C bzXiz2JXiT8JDTBD0f5Zrq1V4SQTSQ82Jplh1n+bUAlejw7Sc2yshYqFMuA6vib6eSgpswF725KMC jEKBm6WA==; Received: from host86-137-89-56.range86-137.btcentralplus.com ([86.137.89.56]:50933 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 1k0Xlm-0003fX-KD; Tue, 28 Jul 2020 18:08:22 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [Committed] middle-end: Parity and popcount folding optimizations. Date: Tue, 28 Jul 2020 23:08:20 +0100 Message-ID: <000601d6652b$9b8e9460$d2abbd20$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdZlKkVyemt+HqKiTd2hnbaHewtDZw== 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.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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" Here is the final version of this patch, implementing Richard's latest suggestion, as committed to mainline. This revision has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new regressions. Many thanks for the helpful feedback about match.pd's BUILT_IN_ iterators; I'll post a follow-up patch to clean-up/make use of this idiom elsewhere in match.pd, as it is much nicer. Cheers, Roger --- -----Original Message----- From: Richard Biener Sent: 23 July 2020 10:02 To: GCC Patches Cc: Roger Sayle Subject: Re: [PATCH] middle-end: Fold popcount(x&4) to (x>>2)&1 and friends. On Thu, Jul 23, 2020 at 10:15 AM Marc Glisse wrote: > > On Wed, 22 Jul 2020, Roger Sayle wrote: > > > Many thanks for the peer review and feedback. I completely agree > > that POPCOUNT and PARITY iterators simplifies things and handle the IFN_ variants. > > Is there a reason why the iterators cannot be used for this one? > > +(for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL > + BUILT_IN_POPCOUNTIMAX) > + parity (BUILT_IN_PARITY BUILT_IN_PARITYL BUILT_IN_PARITYLL > + BUILT_IN_PARITYIMAX) > + (simplify > + (bit_and (popcount @0) integer_onep) > + (parity @0))) I should indeed work fine to even do (simplify (bit_and (POPCOUNT @0) integer_onep) (PARITY @0)) Likewise for the existing +/* popcount(X) == 0 is X == 0, and related (in)equalities. */ (for +popcount (POPCOUNT) (for cmp (le eq ne gt) rep (eq eq ne ne) (simplify (cmp (popcount @0) integer_zerop) (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) you can elide the (for ...) OK with those changes. Richard. > > -- > Marc Glisse diff --git a/gcc/match.pd b/gcc/match.pd index c6ae7a7..a052c9e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5964,25 +5964,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (IFN_FMA @0 @1 @2)))) /* 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)) - (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) - (popcount (bit_ior @0 @1)))) - /* popcount(X) == 0 is X == 0, and related (in)equalities. */ +/* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ +(simplify + (plus (POPCOUNT:s @0) (POPCOUNT:s @1)) + (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) + (POPCOUNT (bit_ior @0 @1)))) + +/* popcount(X) == 0 is X == 0, and related (in)equalities. */ +(for popcount (POPCOUNT) (for cmp (le eq ne gt) rep (eq eq ne ne) (simplify (cmp (popcount @0) integer_zerop) (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) +/* Canonicalize POPCOUNT(x)&1 as PARITY(X). */ +(simplify + (bit_and (POPCOUNT @0) integer_onep) + (PARITY @0)) + +/* PARITY simplifications. */ +/* parity(~X) is parity(X). */ +(simplify + (PARITY (bit_not @0)) + (PARITY @0)) + +/* parity(X)^parity(Y) is parity(X^Y). */ +(simplify + (bit_xor (PARITY:s @0) (PARITY:s @1)) + (PARITY (bit_xor @0 @1))) + +/* Common POPCOUNT/PARITY simplifications. */ +/* popcount(X&C1) is (X>>C2)&1 when C1 == 1<