From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 2E1AA3857828 for ; Sat, 31 Jul 2021 07:55:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2E1AA3857828 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=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=V55jWBdg9D1UqjFExAbw28A2Ogy9o+mQ7f73sq0eN7A=; b=BHD0uD+N0otldP5AgROuG9KVaF IxUW5/XIZ0ZY7XIhvalsoPrEgsN/qmg0mAUMPlujbx0CbIMH+MqNDFMkNa2uKU5XsRgfMCtBvkAd6 E9TBRyqteN3Fsbb6rJGsanO88CCJLRGnoQw0S+/Z2+UulDaOdlol13pO/qNUH5GXRDqgymiuQ26Qp l64YSbEdSyunHSBcTJ6mgCeWvugr8k7ya5wVnn9thvFxeKb3+qvXDbcV1V1GGmAjc9n1y4veo3n0C 8EJW4GRsZIYmjyODT31BPbb6AzPfDXRaAPWmnesBkJEZz897ORgxl/Av09dZE98hAk536sEx4Pfg3 hU7x6Ksw==; Received: from host109-154-46-127.range109-154.btcentralplus.com ([109.154.46.127]:51612 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1m9jqa-00019d-Ei for gcc-patches@gcc.gnu.org; Sat, 31 Jul 2021 03:55:52 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] Optimize x ? bswap(x) : 0 in tree-ssa-phiopt Date: Sat, 31 Jul 2021 08:55:49 +0100 Message-ID: <02ad01d785e1$7b355eb0$71a01c10$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_02AE_01D785E9.DCFC37B0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdeF4MA7KCsrWzLdT7W1GdOetUiNCA== 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=-12.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Sat, 31 Jul 2021 07:55:54 -0000 This is a multipart message in MIME format. ------=_NextPart_000_02AE_01D785E9.DCFC37B0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Many thanks again to Jakub Jelinek for a speedy fix for PR 101642. Interestingly, that test case "bswap16(x) ? : x" also reveals a missed optimization opportunity. The resulting "x ? bswap(x) : 0" can be further simplified to just bswap(x). Conveniently, tree-ssa-phiopt.c already recognizes/optimizes the related "x ? popcount(x) : 0", so this patch simply makes that transformation make general, additionally handling bswap, parity, ffs and clrsb. All of the required infrastructure is already present thanks to Jakub previously adding support for clz/ctz. To reflect this generalization, the name of the function is changed from cond_removal_in_popcount_clz_ctz_pattern to the hopefully equally descriptive cond_removal_in_builtin_zero_pattern. The following patch has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2021-07-31 Roger Sayle gcc/ChangeLog * tree-ssa-phiopt.c (cond_removal_in_builtin_zero_pattern): Renamed from cond_removal_in_popcount_clz_ctz_pattern. Add support for BSWAP, FFS, PARITY and CLRSB builtins. (tree_ssa_phiop_worker): Update call to function above. gcc/testuite/ChangeLog * gcc.dg/tree-ssa/phi-opt-25.c: New test case. Roger -- Roger Sayle NextMove Software Cambridge, UK ------=_NextPart_000_02AE_01D785E9.DCFC37B0 Content-Type: text/plain; name="patchf.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchf.txt" diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c=0A= index c6adbbd..66af902 100644=0A= --- a/gcc/tree-ssa-phiopt.c=0A= +++ b/gcc/tree-ssa-phiopt.c=0A= @@ -66,9 +66,9 @@ static bool minmax_replacement (basic_block, = basic_block,=0A= edge, edge, gphi *, tree, tree);=0A= static bool spaceship_replacement (basic_block, basic_block,=0A= edge, edge, gphi *, tree, tree);=0A= -static bool cond_removal_in_popcount_clz_ctz_pattern (basic_block, = basic_block,=0A= - edge, edge, gphi *,=0A= - tree, tree);=0A= +static bool cond_removal_in_builtin_zero_pattern (basic_block, = basic_block,=0A= + edge, edge, gphi *,=0A= + tree, tree);=0A= static bool cond_store_replacement (basic_block, basic_block, edge, = edge,=0A= hash_set *);=0A= static bool cond_if_else_store_replacement (basic_block, basic_block, = basic_block);=0A= @@ -350,9 +350,8 @@ tree_ssa_phiopt_worker (bool do_store_elim, bool = do_hoist_loads, bool early_p)=0A= early_p))=0A= cfgchanged =3D true;=0A= else if (!early_p=0A= - && cond_removal_in_popcount_clz_ctz_pattern (bb, bb1, e1,=0A= - e2, phi, arg0,=0A= - arg1))=0A= + && cond_removal_in_builtin_zero_pattern (bb, bb1, e1, e2,=0A= + phi, arg0, arg1))=0A= cfgchanged =3D true;=0A= else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1))=0A= cfgchanged =3D true;=0A= @@ -2466,7 +2465,8 @@ spaceship_replacement (basic_block cond_bb, = basic_block middle_bb,=0A= return true;=0A= }=0A= =0A= -/* Convert=0A= +/* Optimize x ? __builtin_fun (x) : C, where C is __builtin_fun (0).=0A= + Convert=0A= =0A= =0A= if (b_4(D) !=3D 0)=0A= @@ -2498,10 +2498,10 @@ spaceship_replacement (basic_block cond_bb, = basic_block middle_bb,=0A= instead of 0 above it uses the value from that macro. */=0A= =0A= static bool=0A= -cond_removal_in_popcount_clz_ctz_pattern (basic_block cond_bb,=0A= - basic_block middle_bb,=0A= - edge e1, edge e2, gphi *phi,=0A= - tree arg0, tree arg1)=0A= +cond_removal_in_builtin_zero_pattern (basic_block cond_bb,=0A= + basic_block middle_bb,=0A= + edge e1, edge e2, gphi *phi,=0A= + tree arg0, tree arg1)=0A= {=0A= gimple *cond;=0A= gimple_stmt_iterator gsi, gsi_from;=0A= @@ -2549,6 +2549,12 @@ cond_removal_in_popcount_clz_ctz_pattern = (basic_block cond_bb,=0A= int val =3D 0;=0A= switch (cfn)=0A= {=0A= + case CFN_BUILT_IN_BSWAP16:=0A= + case CFN_BUILT_IN_BSWAP32:=0A= + case CFN_BUILT_IN_BSWAP64:=0A= + case CFN_BUILT_IN_BSWAP128:=0A= + CASE_CFN_FFS:=0A= + CASE_CFN_PARITY:=0A= CASE_CFN_POPCOUNT:=0A= break;=0A= CASE_CFN_CLZ:=0A= @@ -2577,6 +2583,15 @@ cond_removal_in_popcount_clz_ctz_pattern = (basic_block cond_bb,=0A= }=0A= }=0A= return false;=0A= + case BUILT_IN_CLRSB:=0A= + val =3D TYPE_PRECISION (integer_type_node) - 1;=0A= + break;=0A= + case BUILT_IN_CLRSBL:=0A= + val =3D TYPE_PRECISION (long_integer_type_node) - 1;=0A= + break;=0A= + case BUILT_IN_CLRSBLL:=0A= + val =3D TYPE_PRECISION (long_long_integer_type_node) - 1;=0A= + break;=0A= default:=0A= return false;=0A= }=0A= ------=_NextPart_000_02AE_01D785E9.DCFC37B0 Content-Type: text/plain; name="phi-opt-25.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="phi-opt-25.c" /* { dg-do compile } */=0A= /* { dg-options "-O2 -fdump-tree-optimized" } */=0A= =0A= unsigned short test_bswap16(unsigned short x)=0A= {=0A= return x ? __builtin_bswap16(x) : 0;=0A= }=0A= =0A= unsigned int test_bswap32(unsigned int x)=0A= {=0A= return x ? __builtin_bswap32(x) : 0;=0A= }=0A= =0A= unsigned long long test_bswap64(unsigned long long x)=0A= {=0A= return x ? __builtin_bswap64(x) : 0;=0A= }=0A= =0A= int test_clrsb(int x)=0A= {=0A= return x ? __builtin_clrsb(x) : (__SIZEOF_INT__*8-1);=0A= }=0A= =0A= int test_clrsbl(long x)=0A= {=0A= return x ? __builtin_clrsbl(x) : (__SIZEOF_LONG__*8-1);=0A= }=0A= =0A= int test_clrsbll(long long x)=0A= {=0A= return x ? __builtin_clrsbll(x) : (__SIZEOF_LONG_LONG__*8-1);=0A= }=0A= =0A= #if 0=0A= /* BUILT_IN_FFS is transformed by match.pd */=0A= int test_ffs(unsigned int x)=0A= {=0A= return x ? __builtin_ffs(x) : 0;=0A= }=0A= =0A= int test_ffsl(unsigned long x)=0A= {=0A= return x ? __builtin_ffsl(x) : 0;=0A= }=0A= =0A= int test_ffsll(unsigned long long x)=0A= {=0A= return x ? __builtin_ffsll(x) : 0;=0A= }=0A= #endif=0A= =0A= int test_parity(int x)=0A= {=0A= return x ? __builtin_parity(x) : 0;=0A= }=0A= =0A= int test_parityl(long x)=0A= {=0A= return x ? __builtin_parityl(x) : 0;=0A= }=0A= =0A= int test_parityll(long long x)=0A= {=0A= return x ? __builtin_parityll(x) : 0;=0A= }=0A= =0A= int test_popcount(int x)=0A= {=0A= return x ? __builtin_popcount(x) : 0;=0A= }=0A= =0A= int test_popcountl(long x)=0A= {=0A= return x ? __builtin_popcountl(x) : 0;=0A= }=0A= =0A= int test_popcountll(long long x)=0A= {=0A= return x ? __builtin_popcountll(x) : 0;=0A= }=0A= =0A= /* { dg-final { scan-tree-dump-not "goto" "optimized" } } */=0A= =0A= ------=_NextPart_000_02AE_01D785E9.DCFC37B0--