From patchwork Tue Dec 21 15:08:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 49152 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7374A385803F for ; Tue, 21 Dec 2021 15:09:49 +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 94B03385801C for ; Tue, 21 Dec 2021 15:08:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 94B03385801C 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=3io2HCTTZg4vHqbnerS2SDhtr0W1KkxbOg3uM/tMhCM=; b=jqvi+uaDSZpFpdZ3f8M/PN5JuT HpCP2tW0vdnMSxhmBv7mTEykjS26QPlogayAAutE9YzK5Q4LDB0+jBht+ft499CgpjwHW+d7zU6S8 UWqfP9+rnvhhdODlrF+Qvu6ct9JM3dsA4IlbetOPz5IJLVst8wp/ipfk1ME1hiRKX2BncEIZyGcou 37U7TnrfsIykTUJ0vudcNSOUJ/bLXVd4e2rHwC3KrY06IfqK1P6oZQKw6Gbq3Zvt1Mf4oHAonoJjV Nv/0ycqcywjKNDokkm7UJC9mr0V104TbBk3gCY37J67T2H6VVcYeXlYN4BqiMU44J85JiE7Ub2/43 oinrMoIg==; Received: from [185.62.158.67] (port=49967 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 1mzgkb-0002OU-05; Tue, 21 Dec 2021 10:08:25 -0500 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] x86: Shrink writing 0/-1 to memory using and/or with -Oz. Date: Tue, 21 Dec 2021 15:08:24 -0000 Message-ID: <03be01d7f67c$9a638530$cf2a8f90$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adf2fBasl0Sy8a88T1Gj/EqhlTYKKg== 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.3 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This is the second part of my fix to PR target/103773 where -Oz shouldn't use push/pop on x86 to shrink writing small integer constants to memory. Instead clang uses "andl $0, mem" for writing zero, and "orl $-1, mem" when writing -1 to memory when using -Oz. This patch implements this via peephole2 where we can confirm that its ok to clobber the flags. On the CSiBE benchmark, this reduces total code size from 3664172 bytes to 3663304 bytes, saving 868 bytes. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures, and the new testcase checked both with and without -m32. Ok for mainline? 2021-12-21 Roger Sayle gcc/ChangeLog * gcc/config/i386/i386.md (define_peephole2): With -Oz use andl $0,mem instead of movl $0,mem and orl $-1,mem instead of movl $-1,mem. gcc/testsuite/ChangeLog * gcc.target/i386/pr103773-2.c: New test case. Thanks in advance (and my apologies for the breakage). Roger diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index d25453f..d872824 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -20940,6 +20942,19 @@ DONE; }) +;; Aggressive size optimizations with -Oz +(define_peephole2 + [(set (match_operand:SWI248 0 "memory_operand") (const_int 0))] + "optimize_size > 1 && peep2_regno_dead_p (0, FLAGS_REG)" + [(parallel [(set (match_dup 0) (and:SWI248 (match_dup 0) (const_int 0))) + (clobber (reg:CC FLAGS_REG))])]) + +(define_peephole2 + [(set (match_operand:SWI248 0 "memory_operand") (const_int -1))] + "optimize_size > 1 && peep2_regno_dead_p (0, FLAGS_REG)" + [(parallel [(set (match_dup 0) (ior:SWI248 (match_dup 0) (const_int -1))) + (clobber (reg:CC FLAGS_REG))])]) + ;; Reload dislikes loading constants directly into class_likely_spilled ;; hard registers. Try to tidy things up here. (define_peephole2 diff --git a/gcc/testsuite/gcc.target/i386/pr103773-2.c b/gcc/testsuite/gcc.target/i386/pr103773-2.c new file mode 100644 index 0000000..9dafebd --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr103773-2.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-Oz" } */ +short s; +int i; +long long l; + +void s0() { s = 0; } +void sm1() { s = -1; } +void i0() { i = 0; } +void im1() { i = -1; } +void l0() { l = 0; } +void lm1() { l = -1; } + +/* { dg-final { scan-assembler-not "\tmov\[wlq\]\t\\\$0," } } */ +/* { dg-final { scan-assembler-not "\tmov\[wlq\]\t\\\$-1," } } */ +/* { dg-final { scan-assembler "\tandw\t\\\$0," } } */ +/* { dg-final { scan-assembler "\torw\t\\\$-1," } } */ +/* { dg-final { scan-assembler "\torl\t\\\$-1," } } */ +