From patchwork Thu Jun 11 19:14:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1307701 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=cjAaE5dP; 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 49jYTC4cdGz9sRk for ; Fri, 12 Jun 2020 05:14:55 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F9C739484AE; Thu, 11 Jun 2020 19:14:52 +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 23ABB393C8B2 for ; Thu, 11 Jun 2020 19:14:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 23ABB393C8B2 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: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=1sDrZfzFcvc3Ra14m6ikuh7EWEmqCgvLd66/wfqxRBE=; b=cjAaE5dPdACfv2ySNsM0VOCJBQ xEhMsiHaXAHfJ3Bh6pkJyqgjd18HtrwgJZOqN+9IyTSDCApK6hElk/cATue5AMZLO/og/G5SNpmvM vP7BtWeCar0S3adxB/HdeM89t7WKZ9k+amjk65XPv4zWg4SbTc2l/gunU34bPYuh3tBUjPo+fQQ3U 2flEqVZd64s8P2mPRVFYcwnI/f4qXpn5AcBYnS8Dv3nXGsrapG0Y7tRAxem6qiHd9Fvs4XxP1Q3nE 8HyGwam5oMcPUacWrmdkF+BDLNfhORf7bnPJCoquh1G7wECYXeDd5ZSaJ18pblswyUMp8sQdDNUu3 ec5YlJ/g==; Received: from host86-137-89-123.range86-137.btcentralplus.com ([86.137.89.123]:61023 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 1jjSf3-0001tN-AZ for gcc-patches@gcc.gnu.org; Thu, 11 Jun 2020 15:14:49 -0400 From: "Roger Sayle" To: Subject: [PATCH] middle-end: Optimize (A&C)^(B&C) to (A^B)&C in simplify_rtx. Date: Thu, 11 Jun 2020 20:14:47 +0100 Message-ID: <006501d64024$93550030$b9ff0090$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdZAHUzG57MdDRsYR/KYhcG7ANg3Qw== 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.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 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" My apologies in advance for a middle-end patch without a test case. The patch below implements a simple/safe missing transformation in the RTL optimizers, that transforms (A&C)^(B&C) into the equivalent (A^B)&C, when C doesn't side-effect, such as a constant. I originally identified this opportunity in gfortran, where: integer function foo(i) result(res) integer(kind=16), intent(in) :: i res = poppar(i) end function currently on x86_64 with -O2 -march=corei7 gfortran produces: foo_: popcntq (%rdi), %rdx popcntq 8(%rdi), %rax andl $1, %edx andl $1, %eax xorl %edx, %eax ret But with this patch, now produces: foo_: popcntq (%rdi), %rdx popcntq 8(%rdi), %rax xorl %edx, %eax andl $1, %eax ret The equivalent C/C++ testcase is: unsigned int foo(unsigned int x, unsigned int y) { return __builtin_parityll(x) ^ __builtin_parityll(y); } where GCC currently generates: foo: movl %esi, %eax movl %edi, %edi popcntq %rdi, %rdi popcntq %rax, %rax andl $1, %edi andl $1, %eax xorl %edi, %eax ret and with this patch, it instead generates: foo: movl %esi, %eax movl %edi, %edi popcntq %rdi, %rdi popcntq %rax, %rax xorl %edi, %eax andl $1, %eax ret The trouble is I'm just about to submit a patch to improve constant folding of parity in the middle-end's match.pd, which will generate different RTL code sequences for the above two examples. Hopefully, folks agree it's better to have a RTL optimization that's difficult to test, than not perform this simplification at all. The semantics/correctness of this transformation are tested by the run-time tests in gfortran.dg/popcnt_poppar_2.F90 This patch has been tested with "make bootstrap" and "make -k check" on x86_64-pc-linux-gnu with no regressions. If approved, I'd very much appreciate it if someone could commit this change for me. 2020-06-11 Roger Sayle * simplify-rtx.c (simplify_binary_operation_1): Simplify (X & C) ^ (Y & C) to (X ^ Y) & C, when C is simple (i.e. a constant). Thanks very much in advance, Roger --- Roger Sayle NextMove Software Cambridge, UK diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index 28c2dc6..ccf5f6d 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -3128,6 +3128,17 @@ simplify_binary_operation_1 (enum rtx_code code, machine_mode mode, mode); } + /* Convert (xor (and A C) (and B C)) into (and (xor A B) C). */ + if (GET_CODE (op0) == AND + && GET_CODE (op1) == AND + && rtx_equal_p (XEXP (op0, 1), XEXP (op1, 1)) + && ! side_effects_p (XEXP (op0, 1))) + return simplify_gen_binary (AND, mode, + simplify_gen_binary (XOR, mode, + XEXP (op0, 0), + XEXP (op1, 0)), + XEXP (op0, 1)); + /* Convert (xor (and A B) B) to (and (not A) B). The latter may correspond to a machine insn or result in further simplifications if B is a constant. */