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 9E7C33858D35 for ; Tue, 10 Oct 2023 12:28:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E7C33858D35 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=zeUKlu+bjF+sJV4RgZ6q+1Sa5sCEem70sVPGoeAri3c=; b=N77AT4K54P0gHgreoSYwmt0YN6 dMpWNv1W9Hf3lMva7mDIVOZljeY6G8Z/0OC+zE0+10LzGkMjRIOCdOIlHZCuS78OSLP5WsT/RM14p ndXkSNaxUWOh9ITkwjmwc1lQ891jlrH/WztS8zxZ23XtKndvw2JvbB0fdSxcT24AqBIpHpksnWSBG ELnZ94Dob+6KXIKwAeQocFB+wAvCLNSm1Mcvws1N9Kvc0nUSMpheio1lt5LKjNOGqO/ZV9+xSgs8N fk9H2OGx9B3DZchfRrTXd/fFfNH0iYPBjaa/GABtHfOKyMkszOGfkFpM0JIztp3PaNWLlExz3zNZp s09AI+6A==; Received: from [185.62.158.67] (port=54826 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.1) (envelope-from ) id 1qqBqg-00075S-1t; Tue, 10 Oct 2023 08:28:30 -0400 From: "Roger Sayle" To: Cc: "'Jeff Law'" Subject: [PATCH] Optimize (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) as (and:SI x 1). Date: Tue, 10 Oct 2023 13:28:29 +0100 Message-ID: <049c01d9fb75$47c0bfa0$d7423ee0$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_049D_01D9FB7D.A98527A0" X-Mailer: Microsoft Outlook 16.0 Content-Language: en-gb Thread-Index: Adn7c95hbw5rVsHpQwmqpC+PIxMYJg== 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.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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This is a multipart message in MIME format. ------=_NextPart_000_049D_01D9FB7D.A98527A0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This patch is the middle-end piece of an improvement to PRs 101955 and 106245, that adds a missing simplification to the RTL optimizers. This transformation is to simplify (char)(x << 7) != 0 as x & 1. Technically, the cast can be any truncation, where shift is by one less than the narrower type's precision, setting the most significant (only) bit from the least significant bit. This transformation applies to any target, but it's easy to see (and add a new test case) on x86, where the following function: int f(int a) { return (a << 31) >> 31; } currently gets compiled with -O2 to: foo: movl %edi, %eax sall $7, %eax sarb $7, %al movsbl %al, %eax ret but with this patch, we now generate the slightly simpler. foo: movl %edi, %eax sall $31, %eax sarl $31, %eax ret This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures. Ok for mainline? 2023-10-10 Roger Sayle gcc/ChangeLog PR middle-end/101955 PR tree-optimization/106245 * simplify-rtx.c (simplify_relational_operation_1): Simplify the RTL (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) to (and:SI x 1). gcc/testsuite/ChangeLog * gcc.target/i386/pr106245-1.c: New test case. Thanks in advance, Roger -- ------=_NextPart_000_049D_01D9FB7D.A98527A0 Content-Type: text/plain; name="patchsr.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchsr.txt" diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc=0A= index bd9443d..69d8757 100644=0A= --- a/gcc/simplify-rtx.cc=0A= +++ b/gcc/simplify-rtx.cc=0A= @@ -6109,6 +6109,23 @@ simplify_context::simplify_relational_operation_1 = (rtx_code code,=0A= break;=0A= }=0A= =0A= + /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */=0A= + if (code =3D=3D NE=0A= + && op1 =3D=3D const0_rtx=0A= + && (op0code =3D=3D TRUNCATE=0A= + || (partial_subreg_p (op0)=0A= + && subreg_lowpart_p (op0)))=0A= + && SCALAR_INT_MODE_P (mode)=0A= + && STORE_FLAG_VALUE =3D=3D 1)=0A= + {=0A= + rtx tmp =3D XEXP (op0, 0);=0A= + if (GET_CODE (tmp) =3D=3D ASHIFT=0A= + && GET_MODE (tmp) =3D=3D mode=0A= + && CONST_INT_P (XEXP (tmp, 1))=0A= + && is_int_mode (GET_MODE (op0), &int_mode)=0A= + && INTVAL (XEXP (tmp, 1)) =3D=3D GET_MODE_PRECISION (int_mode) - 1)=0A= + return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx);=0A= + }=0A= return NULL_RTX;=0A= }=0A= =0A= diff --git a/gcc/testsuite/gcc.target/i386/pr106245-1.c = b/gcc/testsuite/gcc.target/i386/pr106245-1.c=0A= new file mode 100644=0A= index 0000000..a0403e9=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.target/i386/pr106245-1.c=0A= @@ -0,0 +1,10 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2" } */=0A= +=0A= +int f(int a)=0A= +{=0A= + return (a << 31) >> 31;=0A= +}=0A= +=0A= +/* { dg-final { scan-assembler-not "sarb" } } */=0A= +/* { dg-final { scan-assembler-not "movsbl" } } */=0A= ------=_NextPart_000_049D_01D9FB7D.A98527A0--