From patchwork Tue Oct 10 12:28:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 150739 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp159616vqb; Tue, 10 Oct 2023 05:29:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdxsDbDVIEOsupmD5V/sALJMI+nexybwPXEtmuYW6bcmC7VEur33VFeNd1NqcUZOtmK92M X-Received: by 2002:a17:907:b0d:b0:9ae:37c2:11b2 with SMTP id h13-20020a1709070b0d00b009ae37c211b2mr14438551ejl.15.1696940946706; Tue, 10 Oct 2023 05:29:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696940946; cv=none; d=google.com; s=arc-20160816; b=xv6byXHDdL3iYqdtHArU5ONUtSX0Ku37Dt3XTXT8YFf4s3t574zaa5WLUCo/jrOGLD ezIY1VpTbQi658c0aDnXq/CbVnUw2/x8L4zfJ1K94GI4ZyDIjmiogh6yDM3/XYJrENyF Re/LexdGximwbPOVW8WLirDjkMfNIWGsmxNMjseAfOGhnFo5lus0jgPuNEFfpYAwfsv+ w2mRkPQgRs/ZyWiP+mAhDrttEdNgql7yL72z8rSSA+QhEP+VbD+NvzlsVV7fKE7uL2QH o1vF4STj0k2EE4lDhjBNDa0Y4/D6+tsDXQcKI/V0voa+kCQGcmNDyDRzMDKzXYyaR8O8 OwpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:thread-index:content-language :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=zeUKlu+bjF+sJV4RgZ6q+1Sa5sCEem70sVPGoeAri3c=; fh=Sy3anwBK6I8K69zm30rjfQ8lZh395VkBfIIxnEUPKpA=; b=V++jf8QkqKmqCfVY4vb7R0xYf9lAbkUH1JLKOSFJ2CTSwMnEwhsiRh6Jc27+iiu529 klIudZDlqflWGw9OVz6E/8zyVxLjSgfHen+5FUbnr/OtaJkV7wChQb9BQKB9Wq/sAFmZ XxUaUtU94VWWhrf0fapuWKubAnBPTU6HqM3ByB7/zzx18WsUTJn9wfNqklsMkAcR8CNc X09IyMxPPS/G2dmmAdD/7CREIrcQYuCEJyHArpFp7cf5C9489N92FJy+ftGgrhE0oj/W RqVkhO+D1Zvl+zbElq8E89Zn8M+JeM6zO9dzgrlzRVCClBilBVLCXgUgxp1jJF9TtOgx m0Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=N77AT4K5; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n20-20020a170906841400b009a1d79a32a2si5209842ejx.1027.2023.10.10.05.29.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 05:29:06 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=N77AT4K5; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 819B93857006 for ; Tue, 10 Oct 2023 12:28:59 +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 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779371549928210327 X-GMAIL-MSGID: 1779371549928210327 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 diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index bd9443d..69d8757 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -6109,6 +6109,23 @@ simplify_context::simplify_relational_operation_1 (rtx_code code, break; } + /* (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) -> (and:SI x 1). */ + if (code == NE + && op1 == const0_rtx + && (op0code == TRUNCATE + || (partial_subreg_p (op0) + && subreg_lowpart_p (op0))) + && SCALAR_INT_MODE_P (mode) + && STORE_FLAG_VALUE == 1) + { + rtx tmp = XEXP (op0, 0); + if (GET_CODE (tmp) == ASHIFT + && GET_MODE (tmp) == mode + && CONST_INT_P (XEXP (tmp, 1)) + && is_int_mode (GET_MODE (op0), &int_mode) + && INTVAL (XEXP (tmp, 1)) == GET_MODE_PRECISION (int_mode) - 1) + return simplify_gen_binary (AND, mode, XEXP (tmp, 0), const1_rtx); + } return NULL_RTX; } diff --git a/gcc/testsuite/gcc.target/i386/pr106245-1.c b/gcc/testsuite/gcc.target/i386/pr106245-1.c new file mode 100644 index 0000000..a0403e9 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr106245-1.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int f(int a) +{ + return (a << 31) >> 31; +} + +/* { dg-final { scan-assembler-not "sarb" } } */ +/* { dg-final { scan-assembler-not "movsbl" } } */