From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7856) id 1B9343857B84; Mon, 4 Jul 2022 06:31:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B9343857B84 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Xi Ruoyao To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8546] loongarch: use -mno-check-zero-division as the default for optimized code X-Act-Checkin: gcc X-Git-Author: Xi Ruoyao X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 554fa149a0b31e822c2f02925173a1d2ec58a678 X-Git-Newrev: d6bedcfcefccfbec0e258e797c393f42ab0882ce Message-Id: <20220704063103.1B9343857B84@sourceware.org> Date: Mon, 4 Jul 2022 06:31:03 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Jul 2022 06:31:03 -0000 https://gcc.gnu.org/g:d6bedcfcefccfbec0e258e797c393f42ab0882ce commit r12-8546-gd6bedcfcefccfbec0e258e797c393f42ab0882ce Author: Xi Ruoyao Date: Sat Jul 2 16:16:44 2022 +0800 loongarch: use -mno-check-zero-division as the default for optimized code Integer division by zero is undefined behavior anyway, and there are already many platforms where neither the GCC port and the hardware do anything to trap on division by zero. So any portable program shall not rely on SIGFPE on division by zero, in both theory and practice. As the result, there is no real reason to cost two additional instructions just for the trap on division by zero with a new ISA. One remaining reason to trap on division by zero may be debugging, especially while -fsanitize=integer-divide-by-zero is not implemented for LoongArch yet. To make debugging easier, keep -mcheck-zero-division as the default for -O0 and -Og, but use -mno-check-zero-division as the default for all other optimization levels. Backport this behavior change for 12.2, so 12.1 will be the only release with a different default. Co-authored-by: Lulu Cheng gcc/ChangeLog: * config/loongarch/loongarch.cc (loongarch_check_zero_div_p): New static function. (loongarch_idiv_insns): Use loongarch_check_zero_div_p instead of TARGET_CHECK_ZERO_DIV. (loongarch_output_division): Likewise. * common/config/loongarch/loongarch-common.cc (TARGET_DEFAULT_TARGET_FLAGS): Remove unneeded hook. * doc/invoke.texi: Update to match the new behavior. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/20101011-1.c (dg-additional-options): add -mcheck-zero-division for LoongArch targets. (cherry picked from commit f150dc1bd11802b70277f0fa209f2d23695a1095) Diff: --- gcc/common/config/loongarch/loongarch-common.cc | 3 --- gcc/config/loongarch/loongarch.cc | 18 +++++++++++++++--- gcc/doc/invoke.texi | 3 ++- gcc/testsuite/gcc.c-torture/execute/20101011-1.c | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/gcc/common/config/loongarch/loongarch-common.cc b/gcc/common/config/loongarch/loongarch-common.cc index 085d3d98f1c..ed3730fce8b 100644 --- a/gcc/common/config/loongarch/loongarch-common.cc +++ b/gcc/common/config/loongarch/loongarch-common.cc @@ -37,7 +37,4 @@ static const struct default_options loongarch_option_optimization_table[] = { OPT_LEVELS_NONE, 0, NULL, 0 } }; -#undef TARGET_DEFAULT_TARGET_FLAGS -#define TARGET_DEFAULT_TARGET_FLAGS MASK_CHECK_ZERO_DIV - struct gcc_targetm_common targetm_common = TARGETM_COMMON_INITIALIZER; diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 48d9ccdc331..d72b256df51 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -2102,6 +2102,19 @@ loongarch_load_store_insns (rtx mem, rtx_insn *insn) return loongarch_address_insns (XEXP (mem, 0), mode, might_split_p); } +/* Return true if we need to trap on division by zero. */ + +static bool +loongarch_check_zero_div_p (void) +{ + /* if -m[no-]check-zero-division is given explicitly. */ + if (target_flags_explicit & MASK_CHECK_ZERO_DIV) + return TARGET_CHECK_ZERO_DIV; + + /* if not, don't trap for optimized code except -Og. */ + return !optimize || optimize_debug; +} + /* Return the number of instructions needed for an integer division. */ int @@ -2110,7 +2123,7 @@ loongarch_idiv_insns (machine_mode mode ATTRIBUTE_UNUSED) int count; count = 1; - if (TARGET_CHECK_ZERO_DIV) + if (loongarch_check_zero_div_p ()) count += 2; return count; @@ -4051,7 +4064,6 @@ loongarch_do_optimize_block_move_p (void) return !optimize_size; } - /* Expand a QI or HI mode atomic memory operation. GENERATOR contains a pointer to the gen_* function that generates @@ -5263,7 +5275,7 @@ loongarch_output_division (const char *division, rtx *operands) const char *s; s = division; - if (TARGET_CHECK_ZERO_DIV) + if (loongarch_check_zero_div_p ()) { output_asm_insn (s, operands); s = "bne\t%2,%.,1f\n\tbreak\t7\n1:"; diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 07b440190c3..e7f0a94a7cf 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -24527,7 +24527,8 @@ Set the cost of branches to roughly @var{n} instructions. @itemx -mno-check-zero-divison @opindex -mcheck-zero-division Trap (do not trap) on integer division by zero. The default is -@option{-mcheck-zero-division}. +@option{-mcheck-zero-division} for @option{-O0} or @option{-Og}, and +@option{-mno-check-zero-division} for other optimization levels. @item -mcond-move-int @itemx -mno-cond-move-int diff --git a/gcc/testsuite/gcc.c-torture/execute/20101011-1.c b/gcc/testsuite/gcc.c-torture/execute/20101011-1.c index 649e168e0b1..d2c0f9ab7ec 100644 --- a/gcc/testsuite/gcc.c-torture/execute/20101011-1.c +++ b/gcc/testsuite/gcc.c-torture/execute/20101011-1.c @@ -1,6 +1,7 @@ /* { dg-options "-fnon-call-exceptions" } */ /* With -fnon-call-exceptions 0 / 0 should not be eliminated. */ /* { dg-additional-options "-DSIGNAL_SUPPRESS" { target { ! signal } } } */ +/* { dg-additional-options "-mcheck-zero-division" { target { loongarch*-*-* } } } */ #ifdef SIGNAL_SUPPRESS # define DO_TEST 0