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 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 Content-Type: multipart/mixed; boundary="----=_NextPart_000_00CE_01D9F773.9F7A9190" 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 List-Id: This is a multipart message in MIME format. ------=_NextPart_000_00CE_01D9F773.9F7A9190 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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. ------=_NextPart_000_00CE_01D9F773.9F7A9190 Content-Type: text/plain; name="patchoz.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchoz.txt" diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc=0A= index 477e6ce..9557bff 100644=0A= --- a/gcc/config/i386/i386.cc=0A= +++ b/gcc/config/i386/i386.cc=0A= @@ -15543,6 +15543,13 @@ ix86_avoid_lea_for_addr (rtx_insn *insn, rtx = operands[])=0A= && (regno0 =3D=3D regno1 || regno0 =3D=3D regno2))=0A= return true;=0A= =0A= + /* Split with -Oz if the encoding requires fewer bytes. */=0A= + if (optimize_size > 1=0A= + && parts.scale > 1=0A= + && !parts.base=0A= + && (!parts.disp || parts.disp =3D=3D const0_rtx)) =0A= + return true;=0A= +=0A= /* Check we need to optimize. */=0A= if (!TARGET_AVOID_LEA_FOR_ADDR || optimize_function_for_size_p (cfun))=0A= return false;=0A= diff --git a/gcc/testsuite/gcc.target/i386/lea-2.c = b/gcc/testsuite/gcc.target/i386/lea-2.c=0A= new file mode 100644=0A= index 0000000..20aded8=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.target/i386/lea-2.c=0A= @@ -0,0 +1,7 @@=0A= +/* { dg-do compile { target { ! ia32 } } } */=0A= +/* { dg-options "-Oz" } */=0A= +int foo(int x) { return x<<2; }=0A= +int bar(int x) { return x<<3; }=0A= +long long fool(long long x) { return x<<2; }=0A= +long long barl(long long x) { return x<<3; }=0A= +/* { dg-final { scan-assembler-not "lea\[lq\]" } } */=0A= ------=_NextPart_000_00CE_01D9F773.9F7A9190--