From patchwork Tue Aug 2 14:59:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 357 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp3035368pxt; Tue, 2 Aug 2022 08:00:25 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tb3KwNeiAA/+sihsZlYyBcWgsZjgMiOpMfaGX+t3h3ZtDDNTfPO8P92padNU9FTlSJPSK+ X-Received: by 2002:a17:906:6d98:b0:715:76eb:9e33 with SMTP id h24-20020a1709066d9800b0071576eb9e33mr16851029ejt.729.1659452425037; Tue, 02 Aug 2022 08:00:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659452425; cv=none; d=google.com; s=arc-20160816; b=Lx+Z7N8JYH2zeI5sp3C3a+god8NpZgDFKh3y477hQsrNmZZJAR2NMqsefSjsCyiwH0 Ar6LdvKcGEa4OFDQTY2cWtNZSVqreinn/vLU0zzDIgPEu1ubOIb+OHxh1RBaV3lGp4n7 Nd1qyNPjFPvNA3qQ+FZSoFVN4tdrqxL/OATVUadDplrmKHWT69I7bVZUrFa0Lj0NQyWj V9N3EingNT9BfRAFwMsG/Wmy4u7tq/UVhnp+qo3LCBKUBv1EgmnvJ9EyHuiWobmdsbuD /nKUSZFy8+8BgeTX2+pLxRIdj7ooA5OhwyM1Jr3VS2p1bwS0qQ32kw/8Q+C1UhOjztJO ywwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=rc8IZF5Wn3Y164Ss7OKMGZp/NRdSvJpiNRm+fnTvo9Y=; b=w6YJ/eCOG+vQ3p1EQLH2wUDLvFyIqk+i6bzrRH2f2A0NJG7/QgEeQHPF8tJ8cnve6K V4nxI31LMkgGsNxANbfSDtP9zkBRBYDAjijFdqVwuL46N7/DvL2eYJ7hHrNReZI/28QB 6QUu0f8bzajQSazhNgThqB6TK11hmmm/6WZcBmrJIJVTdntXeXphP5al44UPf5DCKyQk i80t0OaA6n7ivJXYPDfEh1aBNC8wpBRejmaG2angqZC9wpn2pnS/ngYj0STpFww0tWqS 3RAC/QwIXnfYTSaONjXACaI0yZoYao6kkfy+cQXzOCL5bQLDx9tdY8386GE7oBAqSwHm Dp9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=Vs4x2iwh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hb17-20020a170907161100b0072af2bb7c91si13672817ejc.139.2022.08.02.08.00.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 08:00:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=Vs4x2iwh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 29B69386CE65 for ; Tue, 2 Aug 2022 14:59:41 +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 25C3F38582A7 for ; Tue, 2 Aug 2022 14:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 25C3F38582A7 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=rc8IZF5Wn3Y164Ss7OKMGZp/NRdSvJpiNRm+fnTvo9Y=; b=Vs4x2iwhccXA2JuXM7nUmoacMn rWfuXtit81xVSe4sGKLLySJVWVRD4dB84jbdtlErrKRfXBxGM4BIcQMZQfDHte3wAOb9fymZ+Uoh/ 1vDjcfymaee6Aw/WVe1Y/4rsUWjoegcXRgXZZk9yODoGKqL1nwTPZRiyIZNkFH5csrP2zmrf/68/M sM5+dVqREFnTeXbvph2yjCw3+hakJQ/gVg2nss5Fx2XAG49X9+hCQTZcLiAqF9VnzUJouQjlFcRZE 69xrevOdzWZjly/fk3ZtZk5ZVFQmULjvav5i7oMx3pmySEZyFKveGfi+7YLkS3gafjBGN+P4RnEbA TZJ+rSYg==; Received: from host86-169-41-119.range86-169.btcentralplus.com ([86.169.41.119]:63935 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 1oItMa-0004qw-CM; Tue, 02 Aug 2022 10:59:16 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [x86 PATCH] PR target/47949: Use xchg to move from/to AX_REG with -Oz. Date: Tue, 2 Aug 2022 15:59:14 +0100 Message-ID: <004a01d8a680$6f87dff0$4e979fd0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adimf/12u4r8nV8JR9mfsHMtOBnIgA== 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=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, LIKELY_SPAM_BODY, RCVD_IN_BARRACUDACENTRAL, 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.29 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 Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740061985723396743?= X-GMAIL-MSGID: =?utf-8?q?1740061985723396743?= This patch adds a peephole2 to i386.md to implement the suggestion in PR target/47949, of using xchg instead of mov for moving values to/from the %rax/%eax register, controlled by -Oz, as the xchg instruction is one byte shorter than the move it is replacing. The new test case is taken from the PR: int foo(int x) { return x; } where previously we'd generate: foo: mov %edi,%eax // 2 bytes ret but with this patch, using -Oz, we generate: foo: xchg %eax,%edi // 1 byte ret On the CSiBE benchmark, this saves a total of 10238 bytes (reducing the -Oz total from 3661796 bytes to 3651558 bytes, a 0.28% saving). Interestingly, some modern architectures (such as Zen 3) implement xchg using zero latency register renaming (just like mov), so in theory this transformation could be enabled when optimizing for speed, if benchmarking shows the improved code density produces consistently better performance. However, this is architecture dependent, and there may be interactions using xchg (instead a single_set) in the late RTL passes (such as cprop_hardreg), so for now I've restricted this to -Oz. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, with no new failures. Ok for mainline? 2022-08-02 Roger Sayle gcc/ChangeLog PR target/47949 * config/i386/i386.md (peephole2): New peephole2 to convert SWI48 moves to/from %rax/%eax where the src is dead to xchg, when optimizing for minimal size with -Oz. gcc/testsuite/ChangeLog PR target/47949 * gcc.target/i386/pr47949.c: New test case. Thanks, Roger diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index f1158e1..11629ce 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -3018,6 +3018,18 @@ [(parallel [(set (match_dup 1) (match_dup 2)) (set (match_dup 2) (match_dup 1))])]) +;; Convert moves to/from AX_REG into xchg with -Oz. +(define_peephole2 + [(set (match_operand:SWI48 0 "general_reg_operand") + (match_operand:SWI48 1 "general_reg_operand"))] + "optimize_size > 1 + && (REGNO (operands[0]) == AX_REG + || REGNO (operands[1]) == AX_REG) + && optimize_insn_for_size_p () + && peep2_reg_dead_p (1, operands[1])" + [(parallel [(set (match_dup 0) (match_dup 1)) + (set (match_dup 1) (match_dup 0))])]) + (define_expand "movstrict" [(set (strict_low_part (match_operand:SWI12 0 "register_operand")) (match_operand:SWI12 1 "general_operand"))] diff --git a/gcc/testsuite/gcc.target/i386/pr47949.c b/gcc/testsuite/gcc.target/i386/pr47949.c new file mode 100644 index 0000000..7a58fa4 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr47949.c @@ -0,0 +1,9 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-Oz" } */ + +int foo(int x) +{ + return x; +} + +/* { dg-final { scan-assembler "xchg" } } */