From patchwork Fri Dec 10 18:01:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1566612 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=OAG9Uar3; dkim-atps=neutral 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+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4J9dyF0yZMz9sR4 for ; Sat, 11 Dec 2021 05:01:39 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E2343857027 for ; Fri, 10 Dec 2021 18:01:36 +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 90A63385801D for ; Fri, 10 Dec 2021 18:01:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 90A63385801D 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: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=C52aCq0Y3kG14XjIk0yY28i8U31SABuKVhEC26cXaBs=; b=OAG9Uar3nO/Kiy4TWL+Xxs7Jif jmwQMqdNVuH6PxOVIN+0S30bqqPcKUJfRj/a9wQwMg6im4GTICkIWtGwIeMXl7mxPdqNq/wdjAz8f IEw9xuoGM7s7K33UZsWS4tvFg0m9xLk5FvQtMg+N2hdvH1nAUZv6WdWd3Z65UsF8JRMkT5AzA6jIX VUncNBoUEeDlAsQ9dEFnsPRKnAhqr2Id1P7mtSpcBsc4ieIMkSUKnLG9McdiqoYZ3cse442NSrY5c 3AvpVvEEQeS8JAZ4/j8yxx2pTM5AMisl2CHQuxmby2IxscHBlRLVDxcrO5XqaYSZKfsDLbAo2ai+9 VNYbro+Q==; Received: from [185.62.158.67] (port=61155 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 1mvkCm-00062Y-33 for gcc-patches@gcc.gnu.org; Fri, 10 Dec 2021 13:01:12 -0500 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] PR target/32803: Add -Oz option for improved clang compatibility. Date: Fri, 10 Dec 2021 18:01:10 -0000 Message-ID: <00d701d7edef$ea981100$bfc83300$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adft7u/tUnNS/XU4QzevcYfuwvYLVg== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch adds support for an -Oz command line option, aggressively optimizing for size at the expense of performance. GCC's current -Os provides a reasonable balance of size and performance, whereas -Oz is probably only useful for code size benchmarks such as CSiBE. Or so I thought until I read in https://news.ycombinator.com/item?id=25408853 that clang's -Oz sometimes outperforms -O[23s]; I suspect modern instruction decode stages can treat "pushq $1; popq %rax" as a short uop encoding. Instead of introducing a new global variable, this patch simply abuses the existing optimize_size by setting its value to 2. The only change in behaviour is the tweak to the i386 backend implementing the suggestion in PR target/32803 to use a short push/pop sequence for loading small immediate values (-128..127) on x86, matching the behaviour of LLVM. On x86_64, the simple function: int foo() { return 25; } currently generates with -Os: foo: movl $25, %eax // 5 bytes ret With the proposed -Oz, it generates: foo: pushq $25 // 2 bytes popq %rax // 1 byte ret On CSiBE, this results in a 0.94% improvement (3703513 bytes total down to 3668516 bytes). This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures. Ok for mainline? 2021-12-10 Roger Sayle gcc/ChangeLog PR target/32803 * common.opt (Oz): New command line option. * lto-wrapper.c (merge_and_complain, append_compiler_options): Treat OPT_Oz as synonymous with OPT_Os. * optc-save-gen.awk: Increase maximum value of optimize_size to 2. * opts.c (default_options_optimization) [OPT_Oz]: Handle OPT_Oz just like OPT_Os, except set opt->x_optimize_size to 2. (common_handle_option): Skip OPT_Oz just like OPT_Os. * config/i386/i386.md (*movdi_internal): Use a push/pop sequence for suitable SImode TYPE_IMOV moves when optimize_size > 1. (*movsi_internal): Likewise. gcc/testsuite/ChangeLog PR target/32803 * gcc.target/i386/pr32803.c: New test case. Thanks in advance, Roger diff --git a/gcc/common.opt b/gcc/common.opt index fa0a44f..bf6aed2 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -483,6 +483,10 @@ Og Common Optimization Optimize for debugging experience rather than speed or size. +Oz +Common Optimization +Optimize for space aggressively rather than speed. + Q Driver diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c index 54f642d..7d2b7e5 100644 --- a/gcc/lto-wrapper.c +++ b/gcc/lto-wrapper.c @@ -370,12 +370,14 @@ merge_and_complain (vec &decoded_options, case OPT_Ofast: case OPT_Og: case OPT_Os: + case OPT_Oz: existing_opt = -1; for (j = 0; j < decoded_options.length (); ++j) if (decoded_options[j].opt_index == OPT_O || decoded_options[j].opt_index == OPT_Ofast || decoded_options[j].opt_index == OPT_Og - || decoded_options[j].opt_index == OPT_Os) + || decoded_options[j].opt_index == OPT_Os + || decoded_options[j].opt_index == OPT_Oz) { existing_opt = j; break; @@ -407,6 +409,7 @@ merge_and_complain (vec &decoded_options, level = MAX (level, 1); break; case OPT_Os: + case OPT_Oz: level = MAX (level, 2); break; default: @@ -428,6 +431,7 @@ merge_and_complain (vec &decoded_options, level = MAX (level, 1); break; case OPT_Os: + case OPT_Oz: level = MAX (level, 2); break; default: @@ -725,6 +729,7 @@ append_compiler_options (obstack *argv_obstack, vec opts) case OPT_Ofast: case OPT_Og: case OPT_Os: + case OPT_Oz: break; case OPT_Xassembler: diff --git a/gcc/optc-save-gen.awk b/gcc/optc-save-gen.awk index e363ac7..81c2db0 100644 --- a/gcc/optc-save-gen.awk +++ b/gcc/optc-save-gen.awk @@ -93,7 +93,7 @@ var_opt_char[1] = "optimize_size"; var_opt_char[2] = "optimize_debug"; var_opt_char[3] = "optimize_fast"; var_opt_range["optimize"] = "0, 255"; -var_opt_range["optimize_size"] = "0, 1"; +var_opt_range["optimize_size"] = "0, 2"; var_opt_range["optimize_debug"] = "0, 1"; var_opt_range["optimize_fast"] = "0, 1"; diff --git a/gcc/opts.c b/gcc/opts.c index b16497e..7fba3d7a 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -745,6 +745,15 @@ default_options_optimization (struct gcc_options *opts, opts->x_optimize_debug = 0; break; + case OPT_Oz: + opts->x_optimize_size = 2; + + /* Optimizing for size forces optimize to be 2. */ + opts->x_optimize = 2; + opts->x_optimize_fast = 0; + opts->x_optimize_debug = 0; + break; + case OPT_Ofast: /* -Ofast only adds flags to -O3. */ opts->x_optimize_size = 0; @@ -2609,6 +2618,7 @@ common_handle_option (struct gcc_options *opts, case OPT_Os: case OPT_Ofast: case OPT_Og: + case OPT_Oz: /* Currently handled in a prescan. */ break; diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 4e9fae8..bde22b8 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -2213,7 +2213,14 @@ case TYPE_IMOV: gcc_assert (!flag_pic || LEGITIMATE_PIC_OPERAND_P (operands[1])); if (get_attr_mode (insn) == MODE_SI) - return "mov{l}\t{%k1, %k0|%k0, %k1}"; + { + if (optimize_size > 1 + && TARGET_64BIT + && CONST_INT_P (operands[1]) + && IN_RANGE (INTVAL (operands[1]), -128, 127)) + return "push{q}\t%1\n\tpop{q}\t%0"; + return "mov{l}\t{%k1, %k0|%k0, %k1}"; + } else if (which_alternative == 4) return "movabs{q}\t{%1, %0|%0, %1}"; else if (ix86_use_lea_for_mov (insn, operands)) @@ -2431,6 +2438,14 @@ gcc_assert (!flag_pic || LEGITIMATE_PIC_OPERAND_P (operands[1])); if (ix86_use_lea_for_mov (insn, operands)) return "lea{l}\t{%E1, %0|%0, %E1}"; + else if (optimize_size > 1 + && CONST_INT_P (operands[1]) + && IN_RANGE (INTVAL (operands[1]), -128, 127)) + { + if (TARGET_64BIT) + return "push{q}\t%1\n\tpop{q}\t%q0"; + return "push{l}\t%1\n\tpop{l}\t%0"; + } else return "mov{l}\t{%1, %0|%0, %1}"; diff --git a/gcc/testsuite/gcc.target/i386/pr32803.c b/gcc/testsuite/gcc.target/i386/pr32803.c new file mode 100644 index 0000000..f26f07a --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr32803.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-Oz" } */ + +int foo() +{ + return 25; +} + +/* { dg-final { scan-assembler "push" } } */ +/* { dg-final { scan-assembler "pop" } } */