From patchwork Thu Oct 5 09:06:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 148676 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2016:b0:403:3b70:6f57 with SMTP id fe22csp161874vqb; Thu, 5 Oct 2023 02:07:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFupY3P8H5f8825lBLmFCLAuTgau31Dp32k1XMJ6eVwf4BXfSc370mj8qt3g58lW/OTnsAk X-Received: by 2002:a17:906:1ba1:b0:9b8:7746:f180 with SMTP id r1-20020a1709061ba100b009b87746f180mr4184961ejg.73.1696496837312; Thu, 05 Oct 2023 02:07:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696496837; cv=none; d=google.com; s=arc-20160816; b=OviwCVo2T5QW7mT3cdIuX7qqRjZydfBrqr5LOE39FQnGyFdEEnTAe6KJdb/GwZnXTW bvaxqajRdnknJRkYZY4FuALGHFXDrDnMJbcGO8BHqi9PVhV7kUSfIvs4mnee4UZ0ooH7 9bJZe0fPtvhdWc0hDVlvmakKMizKw2AFB8iXKR8nOBRHTr8fri4WdNE9Ncri3cRyTlcA s+rcctVNEWzU+e+RoX3166bE3jBvkvzTMllSc4hrNLcvvGQxdU6BlvGeJpXCxofak65F PTIqqjD3g67V8rUNqy3sI6HeWL2AmxGZsB83+gaWuDyhdCLI82yZHDaa/+USJG/O+0OY mS7w== 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:content-language:thread-index :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=j1pjGEkHqIW2vda3U7RVikoJxA5MgwpGbfSE7WoNHfo=; fh=ez+UBk19YaOo+lQEyE9porlijlGbJDzUOtzUi3k96eQ=; b=oIsu7TmEb4DybLK2XAe21zcxRRVJX6KBeW65RnEYf0yqS4/hqOOSEv4UuB4bo/f6b4 GoWxxm5128GnIMd0OaCQHGRA+wQayd0j6OosL59mapo3avUdRf7TvccFKS1A25zsyB7j dyV+na4FC9r6k7vDpeRbDhgMAGHxP4A0wD40Rzwrrdf62OC3ZybdHD41YDiOvJ1t1LBg 9pdNYeRoiBn6+V4mKQBNMcLblLMikMjOdMAMrqGxIWc1W86zrAQsAAjPWYiAwX34LP8y 0yCmcQx1n4qFg07fPSXD8bw7S80PLmGzAujkT7DOFocrFpjAeaqYjZMY0Djb43QcNbQL fjiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=BrgeAKFs; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gq26-20020a170906e25a00b009930d1379a0si542689ejb.885.2023.10.05.02.07.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 02:07:17 -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=BrgeAKFs; 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 4833E385CC8D for ; Thu, 5 Oct 2023 09:07:13 +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 1925938582BE for ; Thu, 5 Oct 2023 09:06:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1925938582BE 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=j1pjGEkHqIW2vda3U7RVikoJxA5MgwpGbfSE7WoNHfo=; b=BrgeAKFscNU4Il2Gwqs1xGfP/s PDDEn2pNLsdSsAcEeUMh1hn9C5rFt/+nSD/CEc7Z+ivhng1fjL6m8FwnuzYAfDhxVmKBevnOemDCQ gix29Fzxqt5Zuf8OlxOjWzePOQtLfTgNwWqM/u0ZUpcgempbuWAiDufXBBQ30iUUQn869TGT8Qb4G Mo2PFNq0N+SQ8YqTm6QSp7YLoRNSV5KraLLr21MmBs1aO6qojXEn1+uWHnADs7XAZCrCHu09jlFwk XLBKuSZb0BpegQoO/FAAXbq5HcqZ6lABzsxsqMHbfxhbpExn8XTjv+TSgm98emtLAPuXftPcHeqoV s3jN2lmQ==; Received: from host86-160-20-38.range86-160.btcentralplus.com ([86.160.20.38]:61392 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qoKJY-0003lP-1V; Thu, 05 Oct 2023 05:06:36 -0400 From: "Roger Sayle" To: Cc: "'Uros Bizjak'" Subject: [X86 PATCH] Split lea into shorter left shift by 2 or 3 bits with -Oz. Date: Thu, 5 Oct 2023 10:06:33 +0100 Message-ID: <00cd01d9f76b$3db62990$b9227cb0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adn3arRdICG66RKXRLm6vZ5DtD3drw== 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=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, LIKELY_SPAM_BODY, 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: 1778905867801814512 X-GMAIL-MSGID: 1778905867801814512 This patch avoids long lea instructions for performing x<<2 and x<<3 by splitting them into shorter sal and move (or xchg instructions). Because this increases the number of instructions, but reduces the total size, its suitable for -Oz (but not -Os). The impact can be seen in the new test case: int foo(int x) { return x<<2; } int bar(int x) { return x<<3; } long long fool(long long x) { return x<<2; } long long barl(long long x) { return x<<3; } where with -O2 we generate: foo: lea 0x0(,%rdi,4),%eax // 7 bytes retq bar: lea 0x0(,%rdi,8),%eax // 7 bytes retq fool: lea 0x0(,%rdi,4),%rax // 8 bytes retq barl: lea 0x0(,%rdi,8),%rax // 8 bytes retq and with -Oz we now generate: foo: xchg %eax,%edi // 1 byte shl $0x2,%eax // 3 bytes retq bar: xchg %eax,%edi // 1 byte shl $0x3,%eax // 3 bytes retq fool: xchg %rax,%rdi // 2 bytes shl $0x2,%rax // 4 bytes retq barl: xchg %rax,%rdi // 2 bytes shl $0x3,%rax // 4 bytes retq Over the entirety of the CSiBE code size benchmark this saves 1347 bytes (0.037%) for x86_64, and 1312 bytes (0.036%) with -m32. Conveniently, there's already a backend function in i386.cc for deciding whether to split an lea into its component instructions, ix86_avoid_lea_for_addr, all that's required is an additional clause checking for -Oz (i.e. optimize_size > 1). 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. Additional testing was performed by repeating these steps after removing the "optimize_size > 1" condition, so that suitable lea instructions were always split [-Oz is not heavily tested, so this invoked the new code during the bootstrap and regression testing], again with no regressions. Ok for mainline? 2023-10-05 Roger Sayle gcc/ChangeLog * config/i386/i386.cc (ix86_avoid_lea_for_addr): Split LEAs used to perform left shifts into shorter instructions with -Oz. gcc/testsuite/ChangeLog * gcc.target/i386/lea-2.c: New test case. diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 477e6ce..9557bff 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -15543,6 +15543,13 @@ ix86_avoid_lea_for_addr (rtx_insn *insn, rtx operands[]) && (regno0 == regno1 || regno0 == regno2)) return true; + /* Split with -Oz if the encoding requires fewer bytes. */ + if (optimize_size > 1 + && parts.scale > 1 + && !parts.base + && (!parts.disp || parts.disp == const0_rtx)) + return true; + /* Check we need to optimize. */ if (!TARGET_AVOID_LEA_FOR_ADDR || optimize_function_for_size_p (cfun)) return false; diff --git a/gcc/testsuite/gcc.target/i386/lea-2.c b/gcc/testsuite/gcc.target/i386/lea-2.c new file mode 100644 index 0000000..20aded8 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/lea-2.c @@ -0,0 +1,7 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-Oz" } */ +int foo(int x) { return x<<2; } +int bar(int x) { return x<<3; } +long long fool(long long x) { return x<<2; } +long long barl(long long x) { return x<<3; } +/* { dg-final { scan-assembler-not "lea\[lq\]" } } */