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 499283858D28 for ; Sun, 17 Oct 2021 13:08:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 499283858D28 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: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=pUy2hPnLzEMSoyIbyIZK2fgreCCDx6SClhtO9uZFiAE=; b=P9MYhwcv1a2TEh/g5qIEshU+1C KdVVbOCs1QEuJ5AOGX2Ph2cCr23Zh3tR9+7xswMOgNQpE+EnBpz+bZgmHdEbV31f5NxbfuE5ZPQN3 zbNQ3Ero9owK9DgHveesy+CBK0WnIVAX00ddfHWxvn9FcvT4QZvwCG7D2LKV3w25Fm35I5FBv1hcY OQRrBz2jCcLYPli/8kpB206RxVOpyP900UA7d87CJkLo9hAaQYt5LOtZ8eTcsK8FDBcr7kw/kZ97p 6EwTyx87GG+m2S0Caizd55RmFV0BugB4KlxMN6q7rTvKnbGBu159Qd3YzMku1vbRV6iPYpoRSJ2rL /9i3RPCA==; Received: from [185.62.158.67] (port=54825 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 1mc5tp-0000Zs-Pc; Sun, 17 Oct 2021 09:08:25 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] bfin: Popcount-related improvements to machine description. Date: Sun, 17 Oct 2021 14:08:24 +0100 Message-ID: <001e01d7c358$12267e90$36737bb0$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_001F_01D7C360.73ED3080" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdfDVryBzRF5DJirQPCkYDxj89BEtg== 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.1 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.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: Sun, 17 Oct 2021 13:08:28 -0000 This is a multipart message in MIME format. ------=_NextPart_000_001F_01D7C360.73ED3080 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Blackfin processors support a ONES instruction that implements a 32-bit popcount returning a 16-bit result. This instruction was previously described by GCC's bfin backed using a UNSPEC, but with this patch uses a POPCOUNT:SI rtx to capture the semantics, allowing it to evaluated at compile-time. I've decided to keep the instruction name the same (avoiding any changes to the __builtin_bfin_ones machinery), but have provided popcountsi2 and popcounthi2 expanders so that the middle-end can use this instruction to implement __builtin_popcount (and __builtin_parity). The new testcase ones.c short foo () { int t = 5; short r = __builtin_bfin_ones(t); return r; } previously generated: _foo: nop; nop; R0 = 5 (X); R0.L = ONES R0; rts; with this patch, now generates: _foo: nop; nop; nop; R0 = 2 (X); rts; The new testcase popcount.c int foo(int x) { return __builtin_popcount(x); } previously generated: _foo: [--SP] = RETS; SP += -12; call ___popcountsi2; SP += 12; RETS = [SP++]; rts; now generates: _foo: nop; nop; R0.L = ONES R0; R0 = R0.L (Z); rts; And the new testcase parity.c int foo(int x) { return __builtin_parity(x); } previously generated: _foo: [--SP] = RETS; SP += -12; call ___paritysi2; SP += 12; RETS = [SP++]; rts; now generates: _foo: nop; R1 = 1 (X); R0.L = ONES R0; R0 = R1 & R0; rts; This patch has been tested on a cross-compiler to bfin-elf hosted on x86_64-pc-linux-gnu, but without a toolchain, and shows no regressions in the compile-only parts of the testsuite. Ok for mainline? 2021-10-17 Roger Sayle gcc/ChangeLog * config/bfin/bfin.md (define_constants): Remove UNSPEC_ONES. (define_insn "ones"): Replace UNSPEC_ONES with a truncate of a popcount, allowing compile-time evaluation/simplification. (popcountsi2, popcounthi2): New expanders using a "ones" insn. gcc/testsuite/ChangeLog * gcc.target/bfin/ones.c: New test case. * gcc.target/bfin/parity.c: New test case. * gcc.target/bfin/ones.c: New test case. Thanks in advance, Roger -- ------=_NextPart_000_001F_01D7C360.73ED3080 Content-Type: text/plain; name="patchj3.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchj3.txt" diff --git a/gcc/config/bfin/bfin.md b/gcc/config/bfin/bfin.md=0A= index 1ec0bbb..8b311f3 100644=0A= --- a/gcc/config/bfin/bfin.md=0A= +++ b/gcc/config/bfin/bfin.md=0A= @@ -138,8 +138,7 @@=0A= ;; Distinguish a 32-bit version of an insn from a 16-bit version.=0A= (UNSPEC_32BIT 11)=0A= (UNSPEC_NOP 12)=0A= - (UNSPEC_ONES 13)=0A= - (UNSPEC_ATOMIC 14)])=0A= + (UNSPEC_ATOMIC 13)])=0A= =0A= (define_constants=0A= [(UNSPEC_VOLATILE_CSYNC 1)=0A= @@ -1398,12 +1397,32 @@=0A= =0A= (define_insn "ones"=0A= [(set (match_operand:HI 0 "register_operand" "=3Dd")=0A= - (unspec:HI [(match_operand:SI 1 "register_operand" "d")]=0A= - UNSPEC_ONES))]=0A= + (truncate:HI=0A= + (popcount:SI (match_operand:SI 1 "register_operand" "d"))))]=0A= ""=0A= "%h0 =3D ONES %1;"=0A= [(set_attr "type" "alu0")])=0A= =0A= +(define_expand "popcountsi2"=0A= + [(set (match_dup 2)=0A= + (truncate:HI (popcount:SI (match_operand:SI 1 "register_operand" ""))))=0A= + (set (match_operand:SI 0 "register_operand")=0A= + (zero_extend:SI (match_dup 2)))]=0A= + ""=0A= +{=0A= + operands[2] =3D gen_reg_rtx (HImode);=0A= +})=0A= +=0A= +(define_expand "popcounthi2"=0A= + [(set (match_dup 2)=0A= + (zero_extend:SI (match_operand:HI 1 "register_operand" "")))=0A= + (set (match_operand:HI 0 "register_operand") =0A= + (truncate:HI (popcount:SI (match_dup 2))))]=0A= + ""=0A= +{=0A= + operands[2] =3D gen_reg_rtx (SImode);=0A= +})=0A= +=0A= (define_insn "smaxsi3"=0A= [(set (match_operand:SI 0 "register_operand" "=3Dd")=0A= (smax:SI (match_operand:SI 1 "register_operand" "d")=0A= ------=_NextPart_000_001F_01D7C360.73ED3080 Content-Type: text/plain; name="ones.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="ones.c" /* { dg-do compile } */=0A= /* { dg-options "-O2" } */=0A= =0A= short foo ()=0A= {=0A= int t =3D 5;=0A= short r =3D __builtin_bfin_ones(t);=0A= return r;=0A= }=0A= =0A= /* { dg-final { scan-assembler-not "ONES" } } */=0A= ------=_NextPart_000_001F_01D7C360.73ED3080 Content-Type: text/plain; name="parity.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="parity.c" /* { dg-do compile } */=0A= /* { dg-options "-O2" } */=0A= =0A= int foo(int x)=0A= {=0A= return __builtin_parity(x);=0A= }=0A= =0A= /* { dg-final { scan-assembler "ONES" } } */=0A= ------=_NextPart_000_001F_01D7C360.73ED3080 Content-Type: text/plain; name="popcount.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="popcount.c" /* { dg-do compile } */=0A= /* { dg-options "-O2" } */=0A= =0A= int foo(int x)=0A= {=0A= return __builtin_popcount(x);=0A= }=0A= =0A= /* { dg-final { scan-assembler "ONES" } } */=0A= ------=_NextPart_000_001F_01D7C360.73ED3080--